# A Midland Physics Alliance Graduate School (MPAGS) course — Autumn, 2013

## Course information

This course will give a general introduction to Python programming, useful for all physics postgrads, but with an emphasis on astronomy.

A reasonable level of undergraduate programming experience is assumed.

The sessions are scheduled for 3-5pm on Thursdays, commencing 10th October for 5 weeks, in room A6 in the main Nottingham Physics building, and via Access Node broadcasts to Warwick, Birmingham and Keele.

The course will be formatively assessed by coursework. This will involve 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 will appear below (remove any .txt extensions to import):

## Outline

• Session 1: Introduction to Python
• Session 2: Numerical Python and plotting
• Session 3: Scientific Python
• Session 4: Python for specialists (observers and theorists)
• 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 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.3 and 3.3.0). 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. The best version to use for this course is 2.7, as this is compatible both ways. However, I will explain the differences so you should manage fine with different versions. However, note that a number of useful features were introduced during the development of version 2, so if you should at least try to find a version >= 2.4.

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 pyfits 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 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.

On request, for the benefit of students taking the MPAGS course, I can provide ssh and NX access to a linux server with everything installed. However, this server will only be available for the duration of the course.

## Installing software

### Your Linux distribution package manager

Python will certainly be available to install from your operating system’s package manager (e.g. Yast in Suse, MacPorts in OS X, etc.), which will hopefully provide a reasonably recent version. You will probably find Numpy, Scipy, Matplotlib and many other modules are also available via your package manager (especially if it is a reasonably recent distribution version). However, you will probably need to manually install stsci_python (which provides PyFITS and PyRAF, so is only necessary if these tools are relevant to your work).

### All-in-one stuff

I’m a bit old school, and tend to use Emacs + Bash + Python + Scipy + Numpy + Matplotlib + etc. all installed separately. However, there are some all-in-one packages that look good, and which provide an enhanced python interpreter, which you might like. Note that I haven’t used any of these extensively. Feel free to experiment with these packages if you like. If you have success, I’d like to hear about it!

These probably don’t include stsci_python, so this may still need to be installed separately.

### Python

If you are using a Linux or Mac OSX system, you probably already have Python installed. However, sometimes it can be quite an old version that the operating system uses. Typing ‘python’ at the command prompt will start the interpreter and tell you the version. The recommended version for this course is 2.7, but other versions should work ok.

It is recommended to use your native Python installation, or package manager. However, if those fail you or you want the absolute latest version, Python can be obtained from here: http://www.python.org/download/. Installation should be quite straightforward. When it is installed, typing ‘python’ at the command prompt should indicate the correct version. Different versions of python can happily coexist, and it is usually a bad idea to remove the default one that came with your system. Note that you may need to either make sure the new python executable is in your path ahead of the old one, or create an alias.

#### Line editing, history and tab completion

Life with Python is much better if you can use the arrow keys to edit the current line, browse the command history, and hit tab to complete or get suggestions for commands. This will usually happen automatically, and certainly if you use IPython or something similar. If you can’t do all that in your python, then you need to enable it. There is some useful advice here.

### Numpy, Scipy, Matplotlib, PyTables, SymPy, etc.

The easiest way to get additional modules is to use your operating system’s package manager. If a module is not available in the package manager, you should try using pip:

sudo pip install numpy scipy matplotlib
or, failing that, easy_install:
sudo easy_install numpy scipy matplotlib
. These may ‘just work’, but you might need to install pip first (either using your package manager, or see here). If they don’t seem to be able to communicate with the internet, you may need to set your proxy.

Finally, you could get the latest versions directly from here: http://scipy.org/Download and here: http://matplotlib.sourceforge.net/. Again, installation should be straightforward, either by installation package on OSX or Windows, or by one or two commands on Linux (see the README).

### stsci_python

This provides PyFITS, PyRAF and Numdisplay for astronomers. PyFITS may also be useful for other people who use FITS files.

You could try installing these modules using pip (see above), or get it directly from: http://www.stsci.edu/resources/software_hardware/stsci_python

To use PyRAF, you will need a working installation of IRAF.

## Finding help

### PyTables

6 Responses leave one →
October 7, 2013

The MPAGS website says this course commences on Thu 10th October 3-5 but this site says Tue 12th November 1-3, could you tell me which is correct please? Thanks.

October 7, 2013

Sorry, the dates were changed. The course starts on Thursday 10th October at 3-5pm. I’ll update the page now.

September 18, 2014

Hi Steven, is this course is being offered in 1st Semester 2014 and if so, will I be able to access the sessions from Leicester University?

Thanks.

September 18, 2014

That’s the idea, although I haven’t heard anything about the scheduling of MPAGS lectures yet.