Python tutorial

This is a small Python tutorial. It assumes you have access to the IMADA machines where python is already installed.

Python Basics

The programming assignments in this course will be written in Python, an interpreted, object-oriented language that shares some features with both Java and Scheme (from which R derives as well). This tutorial will walk through the primary syntactic constructions in Python, using short examples. It is not required that you complete this tutorial; it is here for your benefit, to facilitate learning the Python language. More thorough tutorials on python are available at:

Invoking the Interpeter

Like Scheme, Python can be run in one of two modes. It can either be used interactively, via an interpeter, or it can be called from the command line to execute a script. We will first use the Python interpreter interactively.

You invoke the interpreter by entering python at the Unix command prompt.

[dm533@woglinde ~/tutorial]$ python
Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

IPython can be used as a system shell replacement, especially on Windows, which has a minimally capable shell. It offers enhanced introspection, additional shell syntax, code highlighting, tab completion string completion and rich history. It is a component of the SciPy package. The Python interpeter can be used to evaluate expressions, for example simple arithmetic expressions. If you enter such expressions at the prompt (>>>) they will be evaluated and the result wil be returned on the next line.

>>> 1 + 1
2
>>> 2 * 3
6
>>> 2 ** 3
8

The ** operator in the last example corresponds to exponentiation.

Like Java, Python has a built in string type. The + operator is overloaded to do string concatenation on string values.

>>> 'artificial' + "intelligence"
'artificialintelligence'

There are many built-in methods which allow you to manipulate strings.

>>> 'artificial'.upper()
'ARTIFICIAL'
>>> 'HELP'.lower()
'help'
>>> len('Help')
4


Notice that we can use either single quotes ' ' or double quotes " " to surround string.

We can also store expressions into variables.

>>> s = 'hello world'
>>> print s
hello world
>>> s.upper()
'HELLO WORLD'
>>> len(s.upper())
11
>>> num = 8.0
>>> num += 2.5
>>> print num
10.5

In Python, unlike Java or C, you do not have declare variables before you assign to them. However, read this thread for a distinction between global and local variables

Exercise: Learn about the methods Python provides for strings. To do this we will use the dir and help commands:

>>> s = 'abc'

>>> dir(s)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__','__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__','__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'replace', 'rfind','rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

>>> help(s.find)
Help on built-in function find:

find(...) S.find(sub [,start [,end]]) -> int Return the lowest index in S where substring sub is found, such that sub is contained within s[start,end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure.
>> s.find('b')
1

Try out some of the string functions listed in dir (for now, ignore those with underscores '_' around the method name).

Built-in Data Structures

Python comes equipped with some useful built-in data structures, broadly similar to Java's collections package. Lists store a sequence of mutable items:

>>> fruits = ['apple','orange','pear','banana']
>>> fruits[0]
'apple'

We can use the + operator to do list concatenation:

>>> otherFruits = ['kiwi','strawberry']
>>> fruits + otherFruits
>>> ['apple', 'orange', 'pear', 'banana', 'kiwi', 'strawberry']

Python also allows negative-indexing from the back of the list. For instance, fruits[-1] will access the last element 'banana':

>>> fruits[-2]
'pear'
>>> fruits.pop()
'banana'
>>> fruits
['apple', 'orange', 'pear']
>>> fruits.append('grapefruit')
>>> fruits
['apple', 'orange', 'pear', 'grapefruit']
>>> fruits[-1] = 'pineapple'
>>> fruits
['apple', 'orange', 'pear', 'pineapple']

We can also index multiple adjacent elements using the slice operator. For instance fruits[1:3] which returns a list containing the elements at position 1 and 2. In general fruits[start:stop] will get the elements in start, start+1, ..., stop-1. We can also do fruits[start:] which returns all elements starting from the start index. Also fruits[:end] will return all elements before the element at position end:

>>> fruits[0:2]
['apple', 'orange']
>>> fruits[:3]
['apple', 'orange', 'pear']
>>> fruits[2:]
['pear', 'pineapple']
>>> len(fruits)
4

The items stored in lists can be any Python data type. So for instance we can have lists of lists:

>>> lstOfLsts = [['a','b','c'],[1,2,3],['one','two','three']]
>>> lstOfLsts[1][2]
3
>>> lstOfLsts[0].pop()
'c'
>>> lstOfLsts
[['a', 'b'],[1, 2, 3],['one', 'two', 'three']]


Exercise: Play with some of the list functions. You can find the methods you can call on an object via the dir and get information about them via the help command:

>>> dir(list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',
'__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__',
'__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__',
'__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__',
'__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__',
'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse',
'sort']
>>> help(list.reverse)
Help on built-in function reverse:

reverse(...)
    L.reverse() -- reverse *IN PLACE*
>>> lst = ['a','b','c']
>>> lst.reverse()
>>> ['c','b','a']

Note: Ignore functions with underscores "_" around the names; these are private helper methods.

A data structure similar to the list is the tuple, which is like a list except that it is immutable once it is created (i.e. you cannot change its content once created). Note that tuples are surrounded with parentheses while lists have square brackets.

>>> pair = (3,5)
>>> pair[0]
3
>>> x,y = pair
>>> x
3
>>> y
5
>>> pair[1] = 6
TypeError: object does not support item assignment

The attempt to modify an immutable structure raised an exception. This is how many errors will manifest: index out of bounds errors, type errors, and so on will all report exceptions in this way.

The last built-in data structure is the dictionary which stores a map from one type of object (the key) to another (the value). The key must be an immutable type (string, number, or tuple). The value can be any Python data type.

Note: in the example below, the printed order of the keys returned by Python could be different than shown below. The reason is that unlike lists which have a fixed ordering, a dictionary is simply a hash table for which there is no fixed ordering of the keys.

>>> studentIds = {'aria': 42.0, 'arlo': 56.0, 'john': 92.0 }
>>> studentIds['arlo']
56.0
>>> studentIds['john'] = 'ninety-two'
>>> studentIds
{'aria': 42.0, 'arlo': 56.0, 'john': 'ninety-two'}
>>> del studentIds['aria']
>>> studentIds
{'arlo': 56.0, 'john': 'ninety-two'}
>>> studentIds['aria'] = [42.0,'forty-two']
>>> studentIds
{'aria': [42.0, 'forty-two'], 'arlo': 56.0, 'john': 'ninety-two'}
>>> studentIds.keys()
['aria', 'arlo', 'john']
>>> studentIds.values()
[[42.0, 'forty-two'], 56.0, 'ninety-two']
>>> studentIds.items()
[('aria',[42.0, 'forty-two']), ('arlo',56.0), ('john','ninety-two')]
>>> len(studentIds)
3

As with nested lists, you can also create dictionaries of dictionaries.

Python data types also include set that eliminates duplicate entries: >>> set(['a','b','a']) set(['a', 'b'])

Exercise: Use dir and help to learn about the functions you can call on dictionaries.

Exercise: How would you use the dictionary type in order to represent a set (rather than a list) of unique items?

Writing Scripts

Now that you've got a handle on using Python interactively, let's write a simple Python script that demonstrates Python's for loop. Open the file called foreach.py and update it with the following code:

# This is what a comment looks like 
fruits = ['apples','oranges','pears','bananas']
for fruit in fruits:
    print fruit + ' for sale'

fruitPrices = {'apples': 2.00, 'oranges': 1.50, 'pears': 1.75}
for fruit, price in fruitPrices.items():
    if price < 2.00:
        print '%s cost %f a pound' % (fruit, price)
    else:
        print fruit + ' are too expensive!'
At the command line, use the following command in the directory containing foreach.py:

[dm533@woglinde ~/tutorial]$ python foreach.py
apples for sale
oranges for sale
pears for sale
bananas for sale
oranges cost 1.500000 a pound
pears cost 1.750000 a pound
apples are too expensive!

The next snippet of code demonstrates python's list comprehension construction:
nums = [1,2,3,4,5,6]
plusOneNums = [x+1 for x in nums]
oddNums = [x for x in nums if x % 2 == 1]
print oddNums
oddNumsPlusOne = [x+1 for x in nums if x % 2 ==1]
print oddNumsPlusOne
Put this code into a file called listcomp.py and run the script:

[dm533@woglinde ~/tutorial]$ python listcomp.py
[1,3,5]
[2,4,6]

There are two ways you can use Emacs to develop Python code. The most straightforward way is to use it just as a text editor: create and edit Python files in Emacs; then run Python to test the code somewhere else, like in a terminal window. Alternatively, you can run Python inside Emacs: see the options under "Python" in the menubar, or type C-c ! to start a Python interpreter in a split screen. (Use C-x o to switch between the split screens). See here for Emacs keybindings.

For advanced debugging, you may want to use an IDE like Eclipse. In that case, you should refer to PyDev.

Exercise: Write a list comprehension which, from a list, generates a lowercased version of each string that has length greater than five. Solution

Beware of Indendation!


Unlike many other languages, Python uses the indentation in the source code for interpretation. So for instance the script
if 0 == 1: 
    print 'We are in a world of arithmetic pain' 
print 'Thank you for playing' 
will output

Thank you for playing

But if we had written the script as
if 0 == 1: 
    print 'We are in a world of arithmetic pain'
    print 'Thank you for playing'
there would be no output. The moral of the story: be careful how you indent! Its best to use a single tab for indentation.

Writing Functions

You can define your own functions:
fruitPrices = {'apples':2.00, 'oranges': 1.50, 'pears': 1.75}

def buyFruit(fruit, numPounds):
    if fruit not in fruitPrices:
        print "Sorry we don't have %s" % (fruit)
    else:
        cost = fruitPrices[fruit] * numPounds
        print "That'll be %f please" % (cost)

# Main Function
if __name__ == '__main__':        
    buyFruit('apples',2.4)
    buyFruit('coconuts',2)        
Rather than having a main function as in Java, the __name__ == '__main__' check is used to delimit expressions which are executed when the file is called as a script from the command line. Read this thread on how to write main functions.
Save this script as fruit.py and run it:

[dm533@woglinde ~/tutorial]$ python fruit.py
That'll be 4.800000 please
Sorry we don't have coconuts

Be aware that in Python, all argument passing is by value, that is, if Python passes the object (value) to which the variable currently refers, not the variable itself. If the object is changed in the function, the variable binded to it changes as well.

Exercise (for submission): Add some more fruit to the fruitPrices dictionary and add a buyLotsOfFruit(orderList) function which takes a list of (fruit,pound) tuples and returns the cost of your list. If there is some fruit in the list which doesn't appear in fruitPrices it should print an error message and return None. Solution

This function should be defined in a file called buyLotsOfFruits.py. A stub implementation is provided here. Note that the fruitPrices variable must be set exactly as it is in the stub.

Test Case:You can "sanity check" this portion of your code by testing that

buyLotsOfFruits.buyLotsOfFruit([ ('apples', 2.0), ('pears',3.0), ('limes',4.0) ]) == 12.25

Advanced Exercise: Write a quickSort function in Python using list comprehensions. Use the first element as the pivot. The solution should be very short. Solution

Object Basics

Although this isn't a class in object-oriented programming, you'll have to use some objects in the programming projects, and so it's worth covering the basics of objects in Python. An object encapsulates data and provides functions for interacting with that data. Here's an example:

class FruitShop:

    def __init__(self, name, fruitPrices):
        """
            name: Name of the fruit shop
            
            fruitPrices: Dictionary with keys as fruit 
            strings and prices for values e.g. 
            {'apples':2.00, 'oranges': 1.50, 'pears': 1.75} 
        """
        self.fruitPrices = fruitPrices
        self.name = name
        print 'Welcome to the %s fruit shop' % (name)
        
    def getCostPerPound(self, fruit):
        """
            fruit: Fruit string
        Returns cost of 'fruit', assuming 'fruit'
        is in our inventory or None otherwise
        """
        if fruit not in self.fruitPrices:
            print "Sorry we don't have %s" % (fruit)
            return None
        return self.fruitPrices[fruit]
        
    def getPriceOfOrder(self, orderList):
        """
            orderList: List of (fruit, numPounds) tuples
            
        Returns cost of orderList. If any of the fruit are  
        """ 
        totalCost = 0.0             
        for fruit, numPounds in orderList:
            costPerPound = self.getCostPerPound(fruit)
            if costPerPound != None:
                totalCost += numPounds * costPerPound
        return totalCost
    
    def getName(self):
        return self.name

The FruitShop class has some data, the name of the shop and the prices per pound of some fruit, and it provides functions, or methods, on this data. What advantage is there to wrapping this data in a class? There are two reasons: 1) Encapsulating the data prevents it from being altered or used inappropriately and 2) The abstraction that objects provide make it easier to write general-purpose code.

So how do we make an object and use it? Download the FruitShop implementation from here and save it to a file called shop.py. We can use the FruitShop as follows:

import shop

name = 'DM533'
fruitPrices = {'apples':2.00, 'oranges': 1.50, 'pears': 1.75}
myFruitShop = shop.FruitShop(name, fruitPrices)
print myFruitShop.getCostPerPound('apples')

otherName = 'DM811'
otherFruitPrices = {'kiwis':1.00, 'bananas': 1.50, 'peaches': 2.75}
otherFruitShop = shop.FruitShop(otherName, otherFruitPrices)
print otherFruitShop.getCostPerPound('bananas')
Copy the code above into a file called shopTest.py (in the same directory as shop.py) and run it:

[dm533@woglinde ~/tutorial]$ python shopTest.py
Welcome to the DM533 fruit shop
2.0
Welcome to the DM811 fruit shop
1.5

So what just happended? The import shop statement told Python to load all of the functions and classes in shop.py. These import statements are used more generally to load code modules. The line myFruitShop = shop.FruitShop(name, fruitPrices) constructs an instance of the FruitShop class defined in shop.py, by calling the __init__ function in that class. Note that we only passed two arguments in, while __init__ seems to take three arguments: (self, name, fruitPrices). The reason for this is that all methods in a class have self as the first argument. The self variable's value is automatically set by the interpreter; when calling a method, you only supply the remaining arguments. The self variable contains all the data (name and fruitPrices) for the current specific instance, similar to this in Java.

Exercise (for submission): Write a function, shopSmart(orders,shops) which takes an orderList (like the kind passed in to FruitShop.getCostOfOrder and a list of FruitShop and returns the FruitShop where your order costs the least amount in total. Solution

This function should be defined in a file called shopSmart.py. A stub implementation is provided here. Note that the shop.py implementation is provided as a "support" file, so please do not submit it.

Test Case:We will check that, with the following variable definitions:

orders1 = [('apples',1.0), ('oranges',3.0)]
orders2 = [('apples',3.0)]			 
dir1 = {'apples': 2.0, 'oranges':1.0}
shop1 =  shop.FruitShop('shop1',dir1)
dir2 = {'apples': 1.0, 'oranges': 5.0}
shop2 = shop.FruitShop('shop2',dir2)
shops = [shop1, shop2]

The following are true:

shopSmart.shopSmart(orders1, shops).getName() == 'shop1'

and

shopSmart.shopSmart(orders2, shops).getName() == 'shop2'

More Python Tips and Tricks

This tutorial has briefly touched on some major aspects of Python that will be relevant to the course. Here's some more useful tidbits: For details on private attribute see here.

Last modified: Tue Jul 6 21:36:57 CEST 2010