Python for physics
Python
is a programming language
which is increasingly
being used for computation in physics.
Why is this?
- Python is easy to learn.
- Python is a modern, interpreted, object-oriented language.
- Python programs are simple, clean, easily readable.
- Python has a wide range of support packages (or program libraries)
useful for numerical computation.
What does this mean?
- Interpreted means that programs can be run immediately
without any intermediate steps that involve building an application.
With modern computers, there is little speed penalty.
- Object-oriented means that the things that the
program deals with are objects, which can represent objects in the
real world, and have an appropriate behaviour. Thus integer objects
will have most of the behaviour you might expect. A planet object
you construct will have the appropriate behaviour required by your program
(provided you program it appropriately!)
- Numerical means the application of methods that
allow you to solve mathematical problems using computer arithmetic.
In particular, this means that approximations will be used that permit
you to do calculus, mainly integration, using arithmetic on the computer.
Like most modern languages, Python consists of a core language which is
extended by the use of external modules or libraries, which add
functionality. Thus the core language does not know about square roots
or cosines; these are added by using a suitable module. In this case
this would be math but might be a higher-level module which in turn
imports math, such as numpy or visual.
Python is known for having a short learning curve, so that it is appropriate
for courses in physics where the computation is the focus, rather than programming.
Python is also known for one peculiarity: the source code that you write has
its structure determined by “white space”, in particular blank spaces.
This is held to be peculiar by the designers and users of other languages;
on the other hand, good practice in these languages demands that you
format your code like this anyway, so that after a while it seems natural.
Three packages especially useful in the teaching of computational
physics are:
- numpy or NumPy, which
adds array capability for numerical programming.
- visual or VPython, which
permits the construction of simple 3D simulations.
- pylab or Matplotlib
which supports plots..
- scipy (http://www.scipy.org) extends numpy with a range
of numerical methods: linear algebra, integration, special functions, ...
Let me start again. Four packages are ...