This is a brief introduction to Python and how to teach Python. It's associated with the MATH1058 (Operational Research with Mathematical Computing) which is new in 2017/18.
For the teaching aspects I would strongly recommend Greg Wilson's How to Teach Programming (And Other Things) which is freely available.
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.
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.
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.
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.
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.
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.
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. 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.
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 or_comp 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 or_comp
If using Mac or Linux type
source activate or_comp
Then any command launched from the terminal or command prompt will use the packages in this environment.
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:
ipythonnumpymatplotlibscipyspyderpipOther packages that will be useful are
jupyterpytestnumbapandasscikit-learnThe command to install new packages is conda install. So, to install the packages above type (or copy and paste) first
activate or_comp
if on Windows, or
source activate or_comp
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.
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.
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.
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.
There are many ways of getting help. The most useful are:
help(<thing>). Works in the console.<thing>? or <thing>??. Works in the console.If an error occurs, make sure to read the error message. The more you read, the more detail you'll see, and the more it will point to what the problem is. There are useful flowcharts online which can speed up this process.
Spyder provides several ways to debug our code. The simplest is simply to print out your data using python's print function:
x = 5
print(x)
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.
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
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