I have been working with Python since 2007 on various projects. I have mostly used it on prototyping projects, some web development and some data analysis work. Python has changed a lot. However I still need to sometimes go and review the basics. Here are some useful notes that I have taken over many years. In case you also like to remind yourself of some of the basic operations and techniques in Python these might come in handy. I am also interested to know some of the simple Python principles and techniques that you find useful
Python Variables
1 2 |
# Python variable a = [1,'cow', True] |
For deleting a variable you can use delete
1 2 |
# delete variables del a, b |
Python style guide uses underscores for specific purposes _ is used for a throw away variable or for referring to the previous results in the interactive Python. Leading __var__ are usually used for reserved variables. Leading single underscore is usually used for internal variables _var.
Keys in Dictionaries
Python has a very easy to use dictionary data structure. Dictionaries are highly efficient that provide O(1) insertion and read operations (as opposed to O(n) in a list). However, it is very easy to accidentally degrade your dictionary to a list and dramatically impact its efficiency. Let’s assume you like to check to know whether your dictionary contains a key. There are two ways to do that. The first method is checking key in dict
and the second one is key in dict.keys()
. Both will return the correct value. However, the second method is dramatically slower than the first since dict.keys()
is in fact a list and not a dictionary anymore
1 2 3 4 5 6 7 8 9 10 11 12 13 |
>>> d = {'a':1, 'b':2} >>> d['a'] 1 >>> d['b'] 2 >>> 'a' in d #this is the correct way of looking up keys in dictionaries ( O(1) complexity ) True >>> 'a' in d.keys() #this is the incorrect way of looking up keys in dictionaries ( O(n) complexity ) True >>> type(d.keys()) <type 'list'> >>> type(d) <type 'dict'> |
IF Statements
Unlike other programing languages there is no multi branching in Python (no “case” and “switch” statements). Python also uses “elif” for else if
1 2 3 4 5 6 7 8 |
# Flow control a = 1234 if a == '4321': # do this elif a == 1234: # do this else: # do this |
Various Assignment Operations
1 2 3 4 5 |
# various assignment operations in Python a = 1234 a, b = 1234, 4321 a, b = b, a #switching variables without using a third variable [a, b] = [1234,4321] |
Zip and Map
Zip and Map are two of the most useful functions in Python. Zip allows you to make tuples of data from lists. Map allows you to apply functions to tuples.
1 2 3 4 5 6 7 8 9 10 11 12 |
# Zip and Map a = [1, 2, 3] b = [3, 2, 1] c = zip(a,b) print c [(1,3), (2,2), (3,1)] # map allows us to apply a function to each Tuple map(sum, zip(a,b)) [4, 4, 4] |
You can define your own function by using lambda. Below is the same sum function
1 2 |
map(lambda pair:sum(pair), zip(a,b)) [4, 4, 4] |
Python Classes
In Python the __init__
method is called automatically at the instantiation stage. Every time Python instantiates an object it will call this method.
List Comprehensions in Python
List comprehensions are one of the most elegant language constructs in Python. Let’s assume we want to produce a list of all numbers between 0 and 100 that are divisible by both 5 and 3. This can be done by the following code
1 2 3 |
>>> [x for x in range(0,101) if ( (x%3)==0 and (x%5)==0)] [0, 15, 30, 45, 60, 75, 90] |
List Slicing in Python
Python has excellent syntaxt for working with its lists. Lists in Python are all zero-indexed so the first element of a list a = [1,2,3]
can be found by a[0]
. You can also take a range of elements, for example if you like to take the second element all the way to the end you would use a[1:]
. Interestingly if you like to take the second element until the end but excluding the last element you would use a[1:-1]
. List slicing notation also accepts steps, for example if you like to take the former list but slip every other element, the you would use a[1:-1:2]
. You can also use negative steps. See this link for more.
The following code sample allows you to generate bigrams for a sentence
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
>>> sentence = " the quick brown fox jumps over the lazy dog " >>> a = b = sentence.split() >>> c = zip(a[0:-1],b[1:]) >>> c [('the', 'quick'), ('quick', 'brown'), ('brown', 'fox'), ('fox', 'jumps'), ('jumps', 'over'), ('over', 'the'), ('the', 'lazy'), ('lazy', 'dog')] >>> d = map(list, c) >>> d [['the', 'quick'], ['quick', 'brown'], ['brown', 'fox'], ['fox', 'jumps'], ['jumps', 'over'], ['over', 'the'], ['the', 'lazy'], ['lazy', 'dog']] >>> e = map(' '.join, d) >>> e ['the quick', 'quick brown', 'brown fox', 'fox jumps', 'jumps over', 'over the', 'the lazy', 'lazy dog'] |
Object Serialization in Python
Python has two modules for serialization cPickle
and Marshal
. You can compress each serialized object by using the gzip
module.
Raising an Exception in Python
If you like to manually raise an arbitrary exception then you can do the following
1 |
raise Exception("An error occurred!") |