Skip to content

An introduction to scientific computing with Python (MPAGS 2017)

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

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 will be timetabled soon. In previous years these have been distributed as one 1.5-2 hour session per week for five 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 the development of a working Python program related to your studies.

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

Previous year’s materials may be viewed here.

Solutions to the exercises are below. You are strongly advised to avoid consulting these solutions until you have tried the problems yourself!

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

Coursework

Details will appear here.

Outline

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

Preliminaries

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 should ensure that you have a working Python interpreter available, ideally the Anaconda distribution (see below). This may be on your laptop, or by logging into (e.g. via ssh) an available server (e.g., one of your research group’s servers). If you have any difficulties installing and running Python, please ask for help during ā€“ or shortly after ā€“ the first session.

Python currently exists in two stable major versions: 2 and 3 (at time of writing the most recent releases are 2.7.14 and 3.6.2). While Python 2 is still being maintained and remains in general use, most projects have moved over to Python 3 by now. Note that Python 3 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. 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 already have Python installed, but you are not recommended to use them! Instead, install a version of Python specifically for your research, as described below.

Getting Python

I highly recommended installing Python using the Anaconda distribution. This gives you the most convenient route to a standalone Python 3 (or 2) installation with most (if not all) of the modules you need easily available. It is entirely free, unless you want some of the super fast add-ons, and even they are free for academic use.

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>

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

Using your operating system’s own version of Python, or installing Python in any other manner, is just asking for trouble.