1. Introduction: abstraction in mathematics and programming

A core tool of mathematics is to define abstract objects and the operations which apply to them. This approach defines all the basic building blocks which enable us to reason mathematically and perform calculations. We start off with basic objects like numbers and define arithmetic operations on them. As we become more sophisticated, we define more and more complex objects, with appropriately more involved operations: matrices, polynomials, sets, groups, algebras. Being able to reason at the level of abstract objects is essential in making mathematics comprehensible. Consider matrices: linear algebra would be highly impractical at best if we could not define matrix addition and multiplication, and had instead to work directly with sums of products of scalars. More generally, without abstraction the edifice of higher mathematics would rapidly collapse under the weight of its own complexity.

The situation in computer programming is strikingly similar. At the simplest level, the central processing unit of a computer is capable only of a limited set of rather primitive arithmetic and logical operations on a few finite subsets of the integers, and of the floating point numbers. However, on this tiny foundation is built every piece of software in existence, including very sophisticated programs for text manipulation, higher mathematics, sound and video. Moreover, this software is routinely created by ordinary people using relatively modest amounts of effort. How is this possible? It is possible because of the same principle of abstraction which underpins mathematics: more abstract objects and the operations on them are defined in terms of simpler ones.

Think about plotting a graph on a computer. As a 21st century mathematician you don’t write loops over arrays to compute the pixel values that will result in the right curve appearing on a screen, you use a high-level language such as Python which has plotting objects which take in data and perform all of those calculations for you. Where did those plotting objects come from? They are the result of the manipulation of lower-level abstract objects in a chain that eventually ends up with primitive operations on integers and floating point numbers. But, critically, as the person wanting to plot some data you don’t care.

Why, then, as a mathematician should you care about abstraction in programming? There are two key reasons. First, because an understanding of the abstractions on which software is built will give you a better understanding of how that software works and will therefore make you a better user of that software. The second is that making appropriate use of abstractions will make you a better programmer of mathematics and other software. Applied well, objects and abstraction produce software which is easier to write, easier to understand, easier to debug, and easier to extend. Indeed, as with abstraction in mathematics, abstraction in coding is a form of constructive laziness: it simultaneously allows the mathematician to achieve more and do less work.

1.1. Obtaining the right software tools

In order to do the exercises in this book, you’ll need some core software tools. At various points you’ll also need install several more Python packages, but you don’t need to install those right now. The core tools you will need are:

  1. Python version 3.8 or later (3.11 or later recommended).

  2. Git (the revision control system we’re going to use).

  3. A Python-aware text editor or integrated development environment (IDE). Visual Studio Code is recommended, and all the instructions will assume that this is what you are using.

Installation instructions for these pieces of code are provided on this page.

This is not a course about Git, but a minimal familiarity with Git and GitHub will be needed in order to work with the examples. Appendix 2 provides an elementary introduction to these tools for readers not already familiar with them.

1.1.1. Python versions

Python is a living language which regularly releases new versions. One very big transition, which took over a decade, was the switch from version 2 to version 3 of the language. For many years both versions were in widespread use and texts needed to cover their differences. However, Python 2 officially reached end of life on 1 January 2020 and not even critical security updates are now made to that version. You should, therefore, only ever use Python 3, and all references in this book are to that version.

Within Python 3, there is a minor version release approximately every year. Once released, this receives security updates for 5 years. At the time of writing, Python 3.11 is the newest release version, and Python 3.8 is the oldest version that still receives security fixes. The user-facing differences between minor Python versions are usually fairly minimal, so for the purposes of this book it doesn’t matter which of the currently supported versions of Python you use, though it is usually advantageous to use the newest version you can conveniently install.

1.1.2. Running the right Python version

Once we have created a virtual environment, we will be able to run Python just by typing python. However, before we get there, what we need to do in order to run Python differs a little depending on the operating system, how we installed Python, and whether there are multiple versions of Python installed on the computer.

On Windows, the py command acts as a general launcher for Python. The following command will print out the version of Python that it will launch by default:

> py --version

On the author’s computer, this prints Python 3.11.4, which is the version of Python I expect to be using. This means I can launch Python on Windows using just py. If it printed a different version, then I could attempt to force it to use the version I want like this:

> py -3.11 --version

If that fails, then there is an issue with your Python documentation and you need to go back to the start of this section to work out how to install the right version.

On Mac or Linux, the safest way to ensure that you are running the right version of Python is to use the full version number, e.g. python3.11. You can check this with, for example:

$ python3.11 --version

If this fails, then the relevant Python version isn’t (correctly) installed and you will need to use a different version, or install it.

1.2. Setting up a Python virtual environment

Video: setting up your virtual environment.

Imperial students can also watch this video on Panopto

In the course of the exercises, You’re going to create, edit, and install a whole bunch of Python packages. It’s highly desirable have a predictable programming environment in which the experiments you’re doing don’t interfere with anything else for which you might be using Python, and conversely which remains unaffected by any packages you may have installed elsewhere. This separation can be achieved by working in a Python virtual environment, or venv.

A virtual environment is a folder containing a local installation of Python which links back to the Python you installed on your computer. This means that virtual environments behave like separate Python installations for most purposes, but are fast to install and take very little space because they share most of their files with the already installed Python.

1.2.1. Creating a working folder

Start by creating a completely fresh folder for your work on this book. You can call this anything you like. On my computer this is called principles_of_programming. You can create this folder using the Windows File Explorer, Mac Finder, or by typing the following in a terminal:

$ mkdir principles_of_programming

Obviously you replace principles_of_programming with whatever you decide to call the folder. The dollar sign is the command prompt. Its different on some systems, for example, it’s often a greater than sign (>) or a percent symbol (%). The text to the left of the command prompt might also be different depending on which terminal program you are using on which operating system, but we are only concerned with the commands to the right of the prompt.

Hint

Modern operating systems are quite capable of dealing with folder names and file names containing spaces. However, there are many pieces of software (including some Python packages) that don’t correctly deal with spaces in folder and file names. It’s therefore a safer option to avoid spaces and instead to separate words with underscores (_).

1.2.2. Creating the venv

The most straightforward way to create a venv is on the terminal command line, not from within Python itself. This is accomplished using Python’s venv package. The venv has to be given a name. You will want this to be short, but distinctive enough that you know which venv you are using. For example, to create a venv called PoP_venv on Windows, you would type:

> py -3.11 -m venv PoP_venv

while on Mac or Linux you would type:

$ python3.11 -m venv PoP_venv

If you’re using a different version of Python then modify the command according to the discussion in section Section 1.1.2. Don’t forget that the > or $ stands for the command prompt: you don’t type it. This command will create the folder PoP_venv and various subfolders containing things like the Python program itself and space for any packages which you install in the venv. If there was already a file or folder called PoP_venv in the current folder then you’ll get an error, so make sure you choose a new name.

Note

Once the venv is installed and activated, it will be sufficient to type python as the venv will ensure that this is the correct version.

A venv doesn’t usually contain any particularly valuable data, so you should regard them as essentially disposable. In particular, if something goes wrong when creating a venv, just delete it and start again. In the bash or zsh shells you would type:

$ rm -rf PoP_venv

Warning

rm -rf will delete its argument and all its subdirectories without further prompts or warnings. There is no undo operation. Be very careful about what you delete.

1.2.3. Using a venv

If you run Python from the terminal, then the simplest way to use the venv is to source its activate script. If using bash or zsh on Mac or Linux you would type:

$ source PoP_venv/bin/activate

while using bash on Windows you would type:

$ source PoP_venv/Scripts/activate

If using PowerShell on Windows then you type:

> .\PoP_venv\Scripts\activate.ps1

Obviously, you would use the folder name of your venv instead of PoP_venv. In either case, your command prompt will change to indicate that you are now using the venv. It might look something like:

(PoP_venv) $

Any subsequent invocations of Python commands such as python will now use the version from the venv, with access to whatever packages you have installed in that venv. If you are using a terminal shell other than bash or zsh, then see the venv package documentation for the correct activation command.

Hint

The default permissions settings on your Windows computer may not permit you to run the activation script. This can be fixed by running:

> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

For further information, see the official Python venv documentation.

Hint

Venv activation is just for one terminal session. You need to activate the venv every time you open a new terminal, though if you are lucky then Visual Studio Code will notice the venv and activate it for you. If you find that Python can’t find your packages or tests, then the first thing to check is whether you remembered to activate the venv.

1.3. Installing Python packages

Suppose we’ve created and activated a venv, and now there’s a Python package we’d like to have access to. Installation of Python packages is handled by the Python package Pip, which you will usually find pre-installed in your Python installation. Pip has many usage options, which enable a large number of different installation configurations. However, for most users most of the time, a few simple pip commands suffice. As with venv creation, package installation is best accomplished from the terminal and not from within Python itself. Don’t forget to activate the venv!

1.3.1. Installing packages from PyPI

PyPI is the Python Package Index. It is the official download location for publicly released Python packages which aren’t themselves a part of the built-in Python Standard Library. Many important mathematical packages including numpy and sympy are distributed from PyPI. Suppose your venv doesn’t have numpy installed and you need it. You would install it with the following terminal command:

(PoP_venv) $ python -m pip install numpy

It is also possible to invoke pip directly using the command pip3, but there are some circumstances where that might result in pip using the wrong Python installation. The approach used here is safer.

Python packages may depend on other Python packages, so it’s quite likely that pip will install more packages than those you directly asked for. This is necessary if those packages are to actually work.

Pip can also be used to upgrade a package to the latest version:

(PoP_venv) $ python -m pip install --upgrade numpy

1.4. Glossary

IDE
integrated development environment

A program designed to help a software developer write code. An IDE combines a text editor with features such as syntax highlighting and checking, debugging capabilities, revision control interfaces and inbuilt terminal windows.

venv
virtual environment

A lightweight private Python installation with its own set of Python packages installed.