Python Notes (0.14.0)

1. Quick Start / Tutorial

This page is a quick introduction to the Python language. All aspects are covered in more details in other pages.

1.1. The basics

1.1.1. Indentation

Before starting, you need to know that in Python, code indentation is an essential part of the syntax. It is used to delimitate code blocks such as loops and functions. It may seem cumbersome, but it makes all Python code consistent and readable. The following code is incorrect:

a = 1
  b = 2

since the two statements are not aligned despite being part of the same block of statements (the main block). Instead, they must be indented in the same way:

a = 1
b = 2

Here is another example involving a loop and a function (def):

def example():
    #The function starts with an indentation
    for i in [1, 2, 3]:
        # The loop starts with an indentation as well
        print i

# calling the function
example()

1.1.2. The print function (1): the Hello world

In order to print the Hello world string, use the print() function as follows:

>>> print("Hello world")
Hello world

Note that before Python 3, you could use print without brackets:

>>> print "Hello world"
Hello world

More about printing later on.

1.1.3. Variables and Types (Toy example)

In the remaining part of this quick introduction, we will play with a very simple example that consists in computing the surface of a circle.

First, we need to use some variables. In python, there is no need to declare variables and therefore there is no need to declare type either. In Python, you hardly need to bother about types but you may need cast from time to time. For instance, the following lines declare a constant PI and a radius. There are both manipulated as float although we haven’t mention it:

pi = 3.14
R = 1.

Warning

If you write R=1 without a dot after the value, then its type is int. Later, if you need to cast it to a float, you can still use the float() function.

Now, let us do the maths:

surface = pi * R * R

or using the power sign:

surface = pi * R**2

We can now print the content of the surface variable:

>>> print(surface)
3.14

1.1.4. Strings

Of course, a more verbose message would be better but first, we need to show how to manipulate strings. In Python, they can be declared in 3 differents ways:

message = 'The surface of the circle = '
message = "The surface of the circle = "
message = """The surface of the circle = """

In short, strings in double quotes work exactly the same as in single quotes but allow to insert single quote character inside them. With the triple quotes (''' or """), you can specify multi-line strings. Moreover, single quotes and double quotes can be used freely within the triple quotes.

It is easy to print non-homogeneous data types:

>>> print(message , surface)
The surface of the circle = 3.14

Here, notice the , sign. If the surface variable was a string, you could concatenate them using the + sign.

Note

In Python, types are either mutable (can be changed) or immutable. Strings are immutable. For instance, message[0] = ‘a’ is not possible, although you can redeclare it: message = ‘new text’.

1.1.5. The print function (2)

In order to print variables with print() function, as in many other languages (e.g., C) you will need the special character %. Here we use only %s so every variables is casted to a string but you could use the standard symbols (e.g., %E, %d, ...).

print('This is a circle of radius %s' % radius)
print('This is a circle of radius %s and surface %s' % (radius, surface))

When using more than one argument, the print function requires to place the arguments in parentheses (this is a tuple as explained later).

1.2. Modules

As soon as you want to start a project, you will need to copy your code into a file. In python, a file that contains Python code is called a module and it ends with the extension .py. You may notice files ending in pyc. Just ignore them. There are compiled files created by Python.

So, a module is just a file where functions and variables are available.

Let us now come back to our toy example. As you may have noticed, the calculation of the surface will not be precise since we used only 2 digits to define the constant PI. Python has lots of standard modules amongst which the math module. To use a module within your own module, you need to import it:

>>> import math

It contains lots a functions and constants and in particular the PI constant:

>>> math.pi

Now, you can redo the computation and get a more precise value:

surface = math.pi * R**2

Note that there are different ways to import modules or functions from a module. Instead of importing the entire math module, we could have imported only the PI value:

from math import pi

which makes the syntax even simpler:

surface = pi * R**2

If you want to import everything from a module, you can write:

from math import *

You need to be cautious with this last statement. Indeed, you do not know what you are importing. You may have imported a function called surface that will overwrite you own variables or functions.

Note that the import statement can be placed anywhere in your code, however, the:

import *

is forbidden within a class or function. It is allowed at module level only.

See Module for more information about modules.

1.3. Functions

Now, that we know a little bit of Python syntax, it is time to create a re-usable function to compute the circle surface:

from math import pi
def compute_surface(radius, pi=math.pi):
    return pi* radius * radius

note the column at the end of the function declaration and the block indentation. This function has one argument and one optional argument (pi). You can call it as follows:

surface1 = compute_surface(1., pi=3.14)
surface2 = compute_surface(1.)

You can do much more of course (several outputs, variable number of arguments, ...) but that should be enough to start with in this page.

More about function in the Functions section.

1.4. Sequences (data structures)

There are lots of data structures in Python and sequences are of particular interest. Sequences allow to put together items that can be accessed to with indexing and slicing methods. You can find mutable and immutable sequences.

Note

indices like in C starts at 0 (NOT 1)

1.4.1. Lists

Lists are mutable sequence. There are created using the square brackets.

>>> radius_list = [1, 10.]
>>> radius_list[0]
1

When you assign a variable the value of an existing object, Python makes a reference to the existing object. Consider te following example:

>>> a = [1, 2, 3]
>>> b = a
>>> b[0] = 10
>>> b
[10, 2, 3]

a and b are two variables but they refer to the same object in memory.

1.4.2. Tuples

Tuples are immutable sequence. There are created using the normal brackets.

>>> radius_list = (1, 10.)
>>> radius_list[0]

1.4.3. Dicts

Dictionaries are mutable pairs of key/value. The key must be immutable type such as strings or numbers. There are defined using curly brackets.

>>> d = {'key1': [1, 'e', 2, 'f', 'whatever'], 'key2':1}
>>> d['key2']
1

1.4.4. Strings

Strings are sequences of characters. As already mentionned, there are immutable.

1.4.5. Slicing

Let us consider the following list:

a = [1, 2, 3, 4]

that can be generated with the range() function:

a = range(0, 4)

You can access to items in a sequences using an index (starting at zero):

>>> a[0]
1

Note also that you can count down using negative indices:

>>> a[-1]   # equivalent to a[3]
4

You cannot use an index out of range. a[4] or a[-4] are wrong.

Slicing can be used to access to a sub list using this syntax:

a[start:end:step]

If start is not provided, the default value is 0. If end is not provided, the default value is the end of the sequence. If step is not provided, the default value is 1.

An example would be to access the even values of a range:

>>> range(1, 11)[::2]
[0, 2, 4, 6, 8, 10]

1.4.6. summary

sequence type syntax
list mutable []
tuple immutable ()
dict mutable {}
string immutable “”

See also

Sets, Frozensets

1.5. Iterators

Iterators are objects that can be traversed through all the elements of a collection. In python many objects are iterators. The for loop (see next section) iterates through object(s). You can transform an object into an iterator using the iter() builtin function

>>> x = [1,2,3]
>>> ix = iter(x)
>>> ix.next()
1
>>> ix.next()
2
>>> ix.next()
3
>>> ix.next()
StopIteration:

See also

builtins

1.6. Programming (and Functional programming)

Python has the standard loop and if but also functional programming that are less standard (e.g., list comprehension).

Note

the apply, map and filter functions can be replaced by list comprehension.

1.6.1. for loop (and while)

You can loop over a sequence as follows:

radius_list = [1., 10.,]
for radius in radius_list:
    surface = compute_surface(radius)

Note again the indentation that delimits the scope of the loop. Hence, there is no need for an end keyword. The indentation suffices. Note also the way to iterate through the list using the in keyword. Any object that is an iterator (such as sequences) can be used in loops like in the example above. The radius variable is created by the loop and therefore available within the loop.

There is a break statement to stop the iteration of the loop/while loop. You may indeed face a situation in which you need to exit a loop when an external condition is triggered. The break statement causes the program flow to exit the body of the while loop and resume the execution of the program at the next statement after the while loop. The break state- ment can be used to force an early exit from a loop or to implement a loop with a test to exit in the middle of the loop body.

Example:

>>> S = 0
>>> while (S < 100):
...     S += (S+1)**2
...     if s > 100
...         break

Another keyword related to loops is the continue keyword. It returns the control to the beginning of the while loop. Instead of continuing the current iteration and then returning the control to the beginning of the loop, the continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.

There is an optional else statement executed when the loop is over, which is hardly used but it is interesting to know its existence. It is not executed once the loop is finished (either normally or after a break statement):

>>> for i in [1,2,3]:
...    print(i)
... else:
...    print "finished"
1
2
3
finished

builtin functions often used with the for loop are enumerate(), range, xrange. We’ve seen the range function earlier. xrange returns a generator instead of a list, which ,akes it slightly faster and memory more efficient. If you want to loop over a range and print the index you could write:

count = 0
for x in xrange(5,10):
    print(x, count)
    count += 1

but in python, you should use enumerate instead:

for count, x in enumerate(xrange(5,10)):
    print(x,count)

Finally, the while loop works exactly as the for loop. Just replace for by while.

1.6.2. if condition

The following example illustrates the usage of if/elif/else keywords:

radius_list = [1., 10., -1.]

for radius in radius_list:
    if radius > 0:
        this_surface = compute_surface(radius)
        print "The surface of a circle of radius %s is %s" % (a_radius, this_surface)
    elif radius == 0:
        print 'There is no switch statement in Python, but you can use elif keyword as much as you want.'
    else:
        print "The radius is negative!"

Note

The special keyword in is a membership operator is used to find a variable in a sequence and can be used to loop over a sequence.

Note

map and filter can be very useful, and faster, but they were added to the language before list comprehension came along. Anything that map and filter can accomplish can also be done with list comprehensions:

1.6.3. do while loop

There is no such loop in Python. Instead, you can use the while loop (see above).

1.6.4. The pass statement

It is used when a statement is required but you do not want to do anything. Nothing happens when it is called. It is typically used in the init function of classes:

.. doctest::
>>> class Test():
...     def __init__(self):
...         pass

1.6.5. Lambda function

In Python, lambda function can be used to replace a simple function. This lambda function is called an anonymous function because they have no name.

To illustrate its syntax and usage, we start from the function defined earlier:

>>> from math import pi, sqrt
>>> def compute_surface(radius)
...     return pi * radius * radius
>>> compute_surface(sqrt(1./pi))
1

An equivalent code using lambda function is:

>>> from math import pi, sqrt
>>> f = lambda r: pi * r * r
>>> f(sqrt(1./pi))
1

You can use several parameters:

>>> lambda x,y: return x**y

See Functions for more details.

1.6.6. zip

The zip() function takes one or more sequences as arguments and returns a list of tuples. The ith tuple contains the ith values of each sequence. If the input sequences do not have the same length, the list is truncated to the length of the shortest sequence. This function is often used to loop over several sequences:

>>> for x,y in zip([1,2,3], [-1,-2,-3]):
...    print x+y
0
0
0

1.6.7. reduce

The reduce() function is not very common but could be useful. It applies a function of two arguments cumulatively to the items of a sequence. The following example computes the factorial:

>>> reduce(lambda x, y: x*y, [1,2,3,4,5])
120

1.6.8. List comprehension

In Python, there is a special syntax called list comprehension that allows to write loops in a very simple and intuitive way. Consider the following code:

>>> my_result = []
>>> for radius in [1, 2, 3, 4]:
...     my_result.append(compute_surface(radius))

It returns the surface corresponding to radius in the list [1,2,3,4]. A list comprehension allows to rewrite this code in one line:

my_results = [compute_surface(radius) for radius in [1, 2, 3, 4]]

So you can use list comprehension to create simple list. You can also include if condition:

my_results = [compute_surface(radius) for radius in [0, 1, 2, 3, 4] if radius > 0]

You can have nested list comprehension. The following example flattens a nested lists:

>>> nested = [[1, 2], [3, 4, 5]]
>>> [x for s in nested for x in s]
[1, 2, 3, 4, 5]

Note

if you have several for in a list comprehension keep in mind that the order of the for statements is the order you would expect them to be written in a standard for-loop (from the outside inwards).

Another example consist in building pairs from 2 lists:

>>> s1 = [1, 2, 3]
>>> s2 = [4, 5, 6]
>>> [(x, y) for x in s1 for y in s2]
[(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]

>>> [(x,y) for x in s1 for y in s2 if x+y>7]
[(2, 6), (3, 5), (3, 6)]

You can also add if/else within a list comprehension as follows:

[unicode(x.strip()) if x is not None else '' for x in row ]

List comprehension replaces the filter, map and apply functions in newer version of Python.

The next sections describe these 3 concepts. Their usage is not standard anymore but you may still encouter them in some old code or tutorials. If you are not interested, you can jump directly to the next section Exceptions.

1.6.9. filter

The filter() function is often used with the lambda function. Its usage is:

filter(function, sequence)

It returns the items of the sequence for which function(item) is true. For instance the following lambda function returns True if x is even:

f = lambda x: x%2 == 0

which can be used within the filter

>>> f = lambda x: x%2 == 0
>>> filter(f, [0,3,6,9])
[0, 6]

In newer version of Python, list comprehension can be used to replace filter. The following statement are equivalent:

>>> filter(func, seq)
>>> [x for x in seq if func(x)]

1.6.10. map

Map is a special function for cases when you need to do a specific action on every element of a list. It enables you to accomplish this without having to write the loop. In newer version of Python map can be replaced by list comprehension but for book keeping, here is an example:

>>> data = [ 'john', 'peter']
>>> result = map(lambda x: "The name %s is %s letters long" % (x, len(x)), data)
>>> print result
['The name john is 4 letters long', 'The name peter is 5 letters long']

1.6.11. apply

apply() is a function that takes as input a function, a tuple and a dictionary. It then calls the function given the tuple and dictionary as arguments. There is no need to use apply since in newer version of Python it is equivalent to calling a function as follows:

func(*args, **kwargs)

where args is a tuple and kwargs a dictionary. See Functions.

1.7. Exceptions

This section may not be for beginners anymore. You can code without knowing anything about Exception. However, you will see error messages for sure while playing with Python. Error messages are exceptions. In Python, there are many exceptions by default that can be used to catch errors. This is done using the try/except code blocks. Optional finally/else blocks may be used.

>>> try:
...     1./0.       # There is a ZeroDivisionError exception in Python
>>> except ZeroDivisionError, e:
...     print 'Your own error message'
...     print e             # The ZeroDivisionError message
>>> finally:
...     print 'The optional finally block always executes after the try/except blocks. '
...     # This is useful to close a file.
>>> else:
...     print 'The optional else block is executed after the try block (if succeeded).'

If you do not know the type of error, you can use this syntax:

>>> try:
...     1./0.
>>> except e:
...     pass

See Exceptions for more information about Exception, in particular user-defined exceptions.

1.8. Assert

Asserts can be used to test the validity of some values:

assert a > 0

You can add an error message:

>>> age = -1
>>> assert 0 < age < 100, 'The age must be realistic'
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AssertionError: The age must be realistic

Warning

you should use raise or exceptions instead of assert because the optimisation option of python ignore the assert statements.

1.9. An object-oriented language

Python is an object oriented language. Everything is an object (list, generator, exception...). Here is a very simple example that should help you starting with classes. Python offers most of the object-oriented tools that comes with other object-oriented languages from inheritance to operator overloading.

1.9.1. A class example

New class style should inherit from the object class.

class MyClass(object):

    # a class variable
    counter = 0

    # the constructor
    def __init__(self, arg1, arg2=0)
        self.arg1 = arg1
        self.__arg2 = arg2

    def a_method(self):
        print 'something'

    def __str__(self):
        return 'There are %s instances' % self.counter

Here, counter is a class variable shared by all instances whereas arg1 is an instance variable. You then declare instances as follows:

>>> c1 = MyClass(1)
>>> c2 = MyClass(2, arg2=3)
>>> print c1
'There are 2 instances'
>>> c2.counter
2.

Note

all class members (including the data members) are public and all the methods are virtual in Python.

If you use the double underscore prefix such as __arg2, Python uses name-mangling to make it a private variable. However, you can still access to it if you want, so this is not a pure private data.

1.9.2. Inheritance

Here is an example of base and derived classes

class Tree(object):

    def __init__(self, name):
        self.name = name

    def __str__(self):
        return  'name: ' + self.name

class FruitTree(Tree):

    def __init__(self, name, fruit_size)
        super(FruitTree, self).__init__(name)
        self.fruit_size = fruit_size

    def __str__(self):
        msg = super(FruitTree, self).__str__()
        msg += '\nIts fruit size is :'  + self.fruit_size

The inheritance is made thanks to the function super. Since functions are virtual the function __str__ in the Child class replaces the one defined in the Parent class.

See Classes for a detailled overview of classes in Python.

1.10. Misc

1.10.1. Single statement block

In general, a statement ends at the end of the line. There is no request for a special character such as semi column. However, you can use it if you want. The two following lines are equivalent:

a = 1
a = 1;

Of course, there is no interest to add a semi column in such case, which explain why you hardly see the semi column in Python code. However, you may want to write several statements on the same line:

a = 1; b = 2

When a loop or if block statement is made of a single line, you may want to write it that way:

if i > 0: print 1./float(i)

1.10.2. multiple assignment

>>> a, b, c = 1, 2, 3
>>> a
1

1.10.3. Python shell

In a shell, the underscore character retrieves the preceding result:

>>> a = 5.1234
>>> round(_, 2)
5.12

1.10.4. comment

use the # sign anywhere or quotes:

>>> # this is a comment
>>> a = 1 # another one
>>> """ a very long comment ...
... on several lines if needed"""

1.10.5. swapping element

x = 1
y = 2
x, y = y, x

1.10.6. None

Python has a special type called None (e.g. equivalent to NULL in R). An example of None object is the object returned by a function that returns nothing. It is convenient to set a variable to an undefined values.

1.10.7. Boolean

Python has a boolean type. The False and True statement are coded as:

>>> a = False
>>> b= True
>>> a == b
False

See Notes about booleans and logical operators for more details.

1.10.8. integer division

In python 2.x, the integer division may lead to unexpected results:

>>> 5/2
2

If you want the exact result, you should coerce to float:

>>> 5/2.
2