First steps

This is a brief introduction to Python for students on, for example, the MSc in Operational Research at Southampton. For this reason we'll often compare to Visual Basic.

Python

Python is a good general purpose language with lots of tools and libraries available, and it's free. It's a solid choice for learning programming, and for testing new code.

Using Python on University machines

A number of Python tools are available on a standard university desktop machine. We will mostly be using Python through spyder, which allows us to write, run, test and debug python code in one place. To launch spyder, either type spyder in the search bar, or go to Start, then All Programs, then Programming Languages, then Anaconda, then choose spyder. If you see two versions of Anaconda, then pick Anaconda 3.

Using Python on your own machine

As Python is free you can install and run it on any machine (or tablet, or phone) you like. In fact, many will have Python already installed, for the use of other software. However, for programming, it is best to have an installation that all works together, which you can easily experiment with, and which won't break other programs if you change something. For these reasons, we recommend you install the anaconda distribution.

Anaconda

If you have enough bandwidth and time (you will be downloading about 1G of software) then you can use the Anaconda graphical installer. There are two versions of Python: a Python 2.X and a Python 3.X. There are small differences between the two. Everything we show here will work on either version. We will be using the 3.X version.

The Anaconda package installs both the essential Python package and a large amount of useful Python software. It will put a launcher icon on your desktop. Clicking on the launcher will bring up a window listing a number of applications: we will be using spyder as seen below.

miniconda

If you do not want to download all the Python packages, but only the essential ones, there is a smaller version of Anaconda, called miniconda. First, download the miniconda package for your computer. Again, we will be using the 3.X version.

The miniconda package installs the basic Python and little else. There are a number of useful packages that we will use. You can install those using the conda app (either via the launcher, or via the command line). But before doing that, it is best to create an environment to install them in, which you can modify without causing problems.

Environments

Packages may rely on other packages, and may rely on specific versions of other packages in order to work. This can lead to "dependency hell", when you need (for different purposes) package A and package B which rely on conflicting versions of package C.

The answer to this is environments, which allow you to organize your different packages to minimize conflicts. Environments are like folders, and you have one for each project you are working on. That way, you ensure that updating or installing packages for one project does not cause problems for a different project.

To get the most out of environments, we need to use the command line, or terminal.

Terminals

A terminal, or a command prompt window, is a window where commands can be typed in to directly run commands or affect files. On Windows you select the Command Prompt from the Accessories menu or the Anaconda console or Anaconda terminal if available. On Mac or Linux system you open a terminal or an XTerm. Inside the terminal you can change directories using the cd command, and run commands associated with Anaconda or miniconda using the conda command.

Creating the environment

We will create a single environment called labs. If you are running on a Mac or on Linux, open a terminal. If on Windows, use a command prompt. Then type

conda create -n msc_or python=3

This creates the new environment, and installs the basic python package in the python 3.X flavour. It does not activate the environment. In order to work within this environment, if using Windows type

activate msc_or

If using Mac or Linux type

source activate msc_or

Then any command launched from the terminal or command prompt will use the packages in this environment.

Packages

After creating the environment, and activating it, the key packages that need installing (if using miniconda; they are all installed with the full Anaconda) are:

  • ipython
  • numpy
  • matplotlib
  • scipy
  • spyder
  • pip

Other packages that will be useful are

  • jupyter
  • pytest
  • numba
  • pandas
  • scikit-learn

The command to install new packages is conda install. So, to install the packages above type (or copy and paste) first

activate msc_or

if on Windows, or

source activate msc_or

if on Mac or Linux, and then type

conda install ipython numpy matplotlib scipy spyder pip \
 jupyter pytest numba pandas scikit-learn

Note: the '\' backslash character should continue an overly long line: if you are typing and not copying and pasting this should be unnecessary.

This will download and install a lot of additional packages that are needed; just agree and continue.

pulp

Finally, there is one package (pulp) that is not (easily) available through conda. We install that using pip. In the terminal, type

pip install pulp

Note: if doing this on Windows, it may be necessary to use the Anaconda console or terminal to do this.

Spyder

There are many ways of writing and running Python code. If you open a terminal (on Mac or Linux; on Windows, this would be a Command Prompt) you can type python or ipython to launch a very bare bones console. This allows you to enter code which it will then run.

More helpful alternatives are Integrated Development Environments (IDEs) or the notebook. The Jupyter notebook (previously called the IPython notebook) is browser based and very powerful for exploratory computing. To run, type jupyter notebook in the terminal prompt.

However, when just getting started, or when writing large codes, IDEs are a better alternative. A simple IDE is spyder which we will use here. To launch, either select spyder from the appropriate menu, or type spyder at the terminal prompt.

You should then see a screen something like the figure (without the annotations).

The four essential parts of the screen are outlined.

  1. The console (bottom right, marked in blue). You can work interactively here. Code run, either interactively or from the editor, will output any results here. Error messages will be reported here. There are two types of console: a Python console, and an IPython console. Both will run Python code, but the IPython console is easier to use.
  2. The editor (left, marked in red). You can write code to be saved to file or run here. This will suggest problems with syntax and has features to help debug and give additional information.
  3. The inspector (top right, marked in green). Can display detailed help on specific objects (or functions, or...) - the Object inspector, or can display detailed information on the variables that are currently defined - the Variable inspector. Extremely useful when debugging.
  4. The working directory (marked in yellow). When running a code this is the first place that spyder looks. You should ensure that the working directory is set to the location where the file is.

For further information on using and setting up spyder, see this tutorial.

Tab completion

A crucial feature of IPython and spyder that saves time and reduces errors is tab completion. When typing anything, try pressing the tab key. This will either automatically complete the name of the variable (or function, or class), or will present a list of options. This is one way of finding out what functions are available - press tab and it will list them all! By typing the first few characters and then pressing tab, you can rapidly narrow down the options.

Help

There are many ways of getting help. The most useful are:

  • Type help(<thing>). Works in the console.
  • Type <thing>? or <thing>??. Works in the console.
  • Type the name in the Object Inspector. Works in spyder only.
  • Google it. Pay particular attention to the online documentation and sites such as stackoverflow.

Debugging

Spyder provides several ways to debug our code. The simplest is simply to print out your data using python's print function:


In [4]:
x = 5

print(x)


5

This will print out the variable to the terminal. This can be useful if you quickly need to check that a variable has been set to the correct value, but if you have long arrays of data this can be less useful, resulting in a large amount of data being dumped onto your screen in a format that is not so easy to read.

A better way of debugging code in Spyder is by using code cells. You may have come across these before if you've used Matlab or Mathematica. We define a new code cell with #%% - in the screen shot below, you can see that spyder has inserted a horizontal line across the editor above this line to show the new code cell. Individual cells can be executed at a time using the 'run current cell' button (circled). The values of the variables after the execution of the cell can then be investigated using the variable explorer on the right.

Cells are very useful, but unfortunately will not allow us to debug all codes. Later on we shall learn about for loops, structures where the same section of code shall be executed multiple times. If we want to investigate variables within this for loop, we need to use breakpoints.

In the screenshot below, we've circled spyder's debugging toolbar. The blue play/pause button will allow you start running your program in the python debugger, ipdb. You should see the prompt ipdb> appear in the terminal. You can then execute the program line by line using the next button along ('Run current line'). At any point, you can print variables using python's print function in the terminal.

In the screenshot below, we've set a breakpoint in our code (shown as a red circle) on line 7 by double clicking to the left of the line number. We can run our code up to this breakpoint using the fast forward button (the two triangles). This will run our code up it reaches the first break point, then pause the execution before running that line. So in our example, the code in lines 1-6 will execute, but the code will then be paused before executing line 7. We can then print out the value of x at this point. If we press the button again, it will execute the for loop again, allowing us to see how the variables get updated.

Reading list

There's a lot of material related to Python online and in the library. None are essential, but lots may be useful. The particular books recommended as a first look are

  • Langtangen, A Primer on Scientific Programming with Python. Detailed, aimed more towards mathematicians than many others.
  • Newman, Computational Physics. Really aimed at teaching numerical algorithms rather than programming, but there's lots of useful examples at a good level.
  • Scopatz & Huff, Effective Computation in Physics. Covers a lot more material than just Python, not exactly aimed at mathematics, but essential background for computational research in the sciences.
  • Saha, Doing Math with Python. Covers more symbolic mathematics and assumes more Python background, but has lots of excellent exercises at the right level.

Versions

These notes have been constructed using the following versions of Python and related packages:


In [1]:
%load_ext watermark
%watermark -v -m -g -p numpy,scipy,matplotlib,pandas,pulp


CPython 3.6.1
IPython 5.3.0

numpy 1.12.1
scipy 0.19.0
matplotlib 2.0.0
pandas 0.19.2
pulp 1.6.5

compiler   : GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)
system     : Darwin
release    : 14.5.0
machine    : x86_64
processor  : i386
CPU cores  : 4
interpreter: 64bit
Git hash   : 232e7b8bf01cd7ee5e0e9b61e60ba38ae20c5dff