Skip to content

An introduction to scientific computing with Python (MPAGS 2018)

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

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 held each Thursday at 3-5pm for five weeks, starting 18th October 2018. These will be delivered in room B17 (or perhaps occasionally in A6) in the main Nottingham Physics building, and via Access Node broadcasts to various MPAGS and other institutions (Warwick, Birmingham, Leicester, Bristol, Keele, Loughborough, Southampton, …). Each session will last around 1.5 hours.

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

To qualify for MPAGS credits, in addition to attending the majority of lectures you should produce a Python program relevant to your research.

Your program should (as a rough guide)…

  • be written as an importable module (.py file) or Jupyter notebook (.ipynb)
  • do something fairly meaningful: analyse real data or perform a simulation
  • use at least two user functions
  • use at least one of the modules introduced in Sessions 3 – 5
  • produce at least one informative plot
  • comprise >~ 50 lines of actual code (excluding comments and imports)

You should submit the source code (.py/.ipynb file), together with pdf/png files of the output plot(s). You must email me (steven.bamford@nottingham.ac.uk) with subject “MPAGS coursework”, attaching the files or linking to a GitHub repository, etc. Please ensure I have access to private repositories and that any additional requirements to run the code are clear.

The final deadline for coursework submission (in order to qualify for MPAGS credits) is 14 Dec. Intermediate submissions for feedback are encouraged. I offer optional feedback on your work in two stages:

  • initial work submitted by 9 Nov, e.g. a README describing what you intend your code to do and/or a rough outline of the code (classes, functions, snippets, comments, pseudocode)
  • ongoing work submitted by 23 Nov, e.g. a roughly working version of your code, although it may be incomplete or have bugs (but try to make it reasonable and easy to understand!)

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. 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+). 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.