Tuesday, 21 July 2009

The basics of ... Python

Image via Wikipedia

Python (named after the TV series Monty Python's Flying Circus, not the snake) is a high level programming language that aims to have a clear syntax and only one correct way of doing something. This post will look at how it can be used for scientific computing.

Python was written by Guido van Rossum beginning in the late 1980's. He is still one of the developers and is affectionately known as the “Benevolent Dictator for Life” by the Python community. His aim is to create a language that is easy and intuitive, yet as powerful as any other language, is open sourced, has a readable syntax and is suitable for everyday use. Many people claim he has been very successful.

Python is a high level language, abstracting away the underlying architecture of the computer it is run on. It is dynamically typed; variables do not have a type assigned to them at compile time and may freely change during execution. For example, a = 1, a = "1" is perfectly legal whereas it would be illegal in a statically typed language such as C++. Python must run on top of an interpreter. Code is compiled into an intermediate byte code that is run on an interpreter; this makes it similar to Java.
Python is the only major language to use indentation as a way of creating code blocks (examples of code blocks are functions, if statements, etc.). This makes Python code look similar regardless of who wrote it, which increases code readability, one of Guido's main aims.
Python is a multi-paradigm language, supporting Procedural, Functional and Object-oriented styles (as well as having useful features for a bunch of other styles).
Python has a large standard library that provides everything from File functions to Internet Protocols. This 'batteries included' philosophy lets the programmer attack the problem at hand rather than having to build the program from the ground up.

Scientific Programming
The basics of Python don't make it immediately useful as a scientific programming language but it is a very mature language with a large community and this has produced several excellent modules, the main of which are NumPy, SciPy and Matplotlib.
Numpy is the numerical core of SciPy and provides high speed (under the hood the linear algebra is handled by LAPACK) multidimensional arrays and matrices as well as lots of ways of creating, manipulating, slicing and dicing them. SciPy builds on NumPy by providing tools for a broad range of basic scientific functions such as Fast Fourier Transforms, Signal Processing, Stats, Optimization tools and much more. It also including a library called Weave that makes it easier to include C++ code in Python. This compliments other solutions such as SWIG and F2Py (Fortran to Python binding)
Matplotlib is a plotting library that provides an interface very similar to Matlab making it easier to for people to use.

These libraries are not the only ones. Because of it's large community, there are literally dozens of scientific, plotting and visualisation libraries from which to choose. This could make life difficult for the beginner but SciPy is well documented and covers a lot of the basics, allowing the novice to get working quickly. When they are more confident and experienced they can start working out which modules will be more suitable.

Python has been described as 'Executable Pseudocode' because of it's clean, readable syntax. Its dynamic typing, interpreter and garbage collection make it quick to write code and the clean syntax again helps with maintenance. It is an ideal glue language because of its large standard library and easy bindings into other languages. For scientific programming SciPy, NumPy and Matplotlib provide a solid basis allowing you to concentrate on the science and not writing lots of code.
Reblog this post [with Zemanta]


  1. "variables do have a type assigned to them at compile time"

    Should there be a "not" there?

  2. *checks text*
    Yes - you're absolutely right! Thanks :-)

  3. Oh the shame! I'll have to have words with my editor :)

  4. I have a huge interest in learning Python. Already knowing PHP makes it easier, but having guides like this really takes the load off.