Skip to content

An introduction to scientific computing with Python (MPAGS 2015)

Click here for the current version of my Python Course!

Midland Physics Alliance Graduate School (MPAGS) — Course AS1 — Autumn, 2015

Course information

This course will give a general introduction to Python programming, useful for all physics postgrads, but with an emphasis on astronomy. It is primarily aimed at graduate students requiring credits as part of the MPAGS training scheme, but other interested students and staff are welcome to join on request.

A reasonable level of undergraduate programming experience is assumed.

The sessions are scheduled for 2-4pm on Thursdays, commencing 8th October for 5 weeks, in room A6 in the main Nottingham Physics building, and via Access Node broadcasts to various MPAGS and other institutions (Warwick, Birmingham, Bristol, Keele, Leicester, Loughborough, Southampton, …)

The course will be formatively assessed by coursework. This will consist of weekly problems and culminate in developing a working Python program related to your studies.

PDF versions of the slides that accompany the course will appear below as the course progresses.

Solutions to the exercises are below.

The exercise solutions and some examples shown in lectures can be found on Github.


Click here to setup a GitHub repo for submitting your coursework.

The coursework requirements are described here.


  • Session 1: Introduction to Python
  • Session 2: Numerical Python and plotting
  • Session 3: Scientific Python
  • Session 4: Python for specialists
  • Session 5: Extreme Python


The course will be run in a ‘workshop’ style. Students are strongly encouraged to bring laptops and try out the examples given during the lecture. Furthermore, there will be time during the scheduled slots for students to work together on exercises, with the lecturer present to give advice.

Before the course begins, you are strongly advised to ensure that you have a working Python interpreter available, e.g. installed on your laptop, or by logging (e.g. via ssh) into an available server (e.g., your University’s UNIX service or one of your research group’s servers).

Python currently exists in two stable major versions: 2 and 3 (at time of writing the most recent releases are 2.7.8 and 3.4.1). Python 2 is stable, still being maintained, and remains in more general use. Python 3 is the future, and is ready for use, but it is not backward compatible with Python 2 due to a small number of significant changes, i.e. code that works with Python 2 will not necessarily work with Python 3. However, the latest version of Python 2 (2.7+), has had many of the features of Python 3 back-ported, so most code should work unchanged in either. I will explain some of the differences so you should be able to cope with different versions.

For this course you are strongly recommended to use a recent version (3.4+ or 2.7+). Linux and OSX include Python, but they are often outdated and you are recommended to leave them alone! Instead, install a version of Python specifically for your research, as described below.

The first session of the course only deals with ‘pure’ Python, but thereafter it relies on a number of third-party modules, which may need to be installed separately (although this is usually quite straightforward). Session 2 introduces numpy and matplotlib, while session 3 covers scipy. In session 4 we use some more specialist tools, which you may only want to install if they are directly useful for your work. Astronomical observers may like to install astropy and pyraf (or ensure they have access to a server with these installed); note that pyraf requires IRAF to be installed. Those interested in symbolic mathematics may like to obtain sympy and Sage (a separate software package based on Python), although this can be also be tried out using a publicly available online tool. Finally, session 5 introduces a number of other modules, including emcee, pytables, numexpr and cython, which those with computationally demanding projects may like to experiment with. The Django web framework will be briefly highlighted, but only those wanting to develop their own web applications need install it.

See the below for installation advice.

Installing software

All-in-one distributions

For those of you who are not observational astronomers, my recommended way of installing Python is to use the Anaconda distribution. These give you the most convenient route to a standalone Python 3 installation with most (if not all) of the modules you need. It is entirely free, unless you want some of the super fast add-ons, which are still free for academic use. An similar alternative is Enthought Canopy. These don’t include PyRAF or other astronomy-specific tools; if you need them you will need to install them separately, although that isn’t so difficult.

If you are an observational astronomer, and particularly if you will be using IRAF/PyRAF, then you should consider Ureka. This distribution includes Python, many scientific modules, and a large amount of astronomy-specific software, including IRAF. However, it does not come with quite as many bells and whistles as Anaconda, although these can be installed separately.

If you are missing a Python module, you can usually get it with one of the following (in the order you should try them):

  • conda install <modulename>
  • pip install <modulename>
  • easy_install <modulename>

Beware that you can have multiple versions of pip and easy_install on one computer, each associated with a particular Python distribution (and perhaps a specific Anaconda environment). Make sure are using the correct one!

If for some reason you don’t like these options, you can try installing Python and the required modules via your operating system’s package manager or manually. Some guidance is available on the webpage for a previous year’s course.

Some useful links

If Googling fails…

No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS