Using Python for mathematics
Given that Python is an interpreted programming language, rather than a fast compiled language, many people do not consider it for writing programs that involve extensive numerical work. While Python programs are certainly slower to execute than the equivalent written in something like C or FORTRAN, mathematical functionality certainly exists in Python and has the inherent advantages of the language; it is easy for people to use and conveniently links to other helpful data structures. Of course speed of calculation may not be so important, for a scientific investigation it may not matter if something takes 1 second or 0.1 second to run. Fortunately, computers get faster and the Python interpreter becomes improved, so you can do quite a bit of numerical work without concern. However, if calculation speed really is important in a given situation then there are a few things you can do to make things faster while still keeping the convenience of Python. For example, you can write code in C, a very efficient numerical language, and use it from within Python (this is called a C extension), effectively extending the vocabulary of the interpreted language with speedy subroutines. More recently the language Cython has helped make C extensions very easy to write. Cython is a Python-like language, and virtually all Python programs can be interpreted by it, without alteration, but the language ultimately generates C code that can be compiled. Cython can be used to call fast library code written in pure C, and can incorporate a mixture of Python and C data structures in the same code; although less flexible, the C data structures are very efficient. Writing C extensions and Cython modules is discussed in Chapter 27.
Python includes standard arithmetic operations as part of the core functionality: add, multiply etc. There is an additional module, math, which always comes packaged with Python and which provides further numerical functionality: logarithms, trigonometry etc. For numerical calculations that are not especially intensive, the core functionality and the math module will often suffice. There has been a history of trying to provide modules for quick numerical algorithms in Python. The first attempt, begun in 1995, was called Numeric, and the second attempt was called Numarray. These two are now deemed to be obsolete, but the third attempt, begun in 2005, is called NumPy (http://numpy.scipy.org/), incorporates elements from the earlier attempts and will hopefully last longer.