Python 3 Language for Cmput 455

Python 3 is a high level language that we will use for teaching the algorithms in this course. It is (by far) not as efficient as C++ for example, but it can express algorithms concisely.

You need to learn enough about Python to understand the algorithms discussed in class, and understand similar algorithms expressed either in Python, or in pseudo code as in our textbook.

You are required to learn how to run a given Python code, make modifications to it or write small new functions, and run it again.

We highly recommend that you work in-depth with the provided code in order to learn more about how the algorithms work.

Python Versions

Unfortunately, there are two different Python dialects in current use, the 2.x line and the 3.x line. See Python 2 vs 3 and main differences. Many systems have both. For example, the Ubuntu 14.04 foundation system currently installed on the department's undergrad machines has both Python 2.7.6 (using the command python2) and Python 3.4.3 (named python3).

We use Python 3 in this course. For the purposes of this course, any reasonable 3.x version should work. We will generally test both our own and your code on the machines in the undergraduate labs or an equivalent machine. These machines currently run Python 3.4.3. (The instructor's laptop is currently running Python 3.5.1).

Cmput 455 Sample Code and Bootcamp

Installing Python 3 and NumPy

Most versions of Unix today come with Python 3 pre-installed. For example, Linux (including the machines in our undergraduate labs) and Mac OS X have it.

To run Python 3, just go to a command line prompt and type

to start.

Note that your instructor and TAs do not provide support for the Python language (beyond fixing our own sample codes if there is a bug).

Downloading and Installing Python 3 on Other Machines

If your computer does not come with Python, you can find a download at the Python Software Foundation's webpage. Pick the recommended 3.x version for your machine. This website is also an excellent resource for all things Python, such as tutorials for the language. Also, you can search the internet for ``interactive python'' to get many tutorials as well as sites that can directly run your Python code in a web browser. has specific install instructions for

Installing pip

Installing pip3: In Linux you can install pip with:

sudo apt-get install python3-pip

On Mac with Python 3.4 and up, pip is pre-installed. Otherwise, see advice on stackoverflow.

For Windows see advice on

Downloading and Installing NumPy

The NumPy library is needed for much of our sample code, including the Go programs and the machine learning examples.

Profiling Tools in Python

For using a profiler in Python there are multiple options. Here we will introduce three of them. cProfile is built-in, while you need to install the other two.


cProfile (Python documentation) is a built-in module for profiling in Python. You don’t need to install anything, and we have tested the functionality under Linux, Windows, and Mac. For profiling using cProfile do the following:

import cProfile

def my_sample_func(x):
    your code here..."my_sample_func(x)") 

This will profile the run of function my_sample_func with argument x. Note the quotation marks.

You can see the example from class in

Another handy way to use cProfile is from the command line. To profile, you type on the command line:

python3 -m cProfile ./my_code

Using this method you will see the overhead of profiling in the report that is generated.


Profilehooks (github site) is not a built-in module. You can install it using pip, or download it from the git repository and use it. To install with pip:

pip3 install profilehooks

To install it without root access, if you are unwilling or unable to give root permission:

pip3 --user install profilehooks

One nice thing about profilehooks is that you can use it as a decorator. To profile the same function as before:

from profilehooks import profile

def my_sample_func(x):
    your code here...



The pycallgraph profiler produces graphs, which makes it interesting to use. You can use it in your code or from the command line. Command line example:

pycallgraph graphviz -- ./

To install with pip:

pip3 install pycallgraph

Or again without root permission:

pip3 --user install pycallgraph

pycallgraph requires graphviz which you can install with:

sudo apt-get install graphviz

These notes have been compiled by Farhad Haqiqat and Martin Müller.