Department of Mathematics FAS Harvard University One Oxford Street Cambridge MA 02138 USA Tel: (617) 495-2171 Fax: (617) 495-5132
FAS Computer Services to Harvard University IT: (617) 495-7777.
Python is an interpreted, interactive, object-oriented programming language. For comparisons with other languages, see here. One of the strenghts of Python is that program development using Python is said to be 5-10 times faster than with C/C++ and 3-5 times faster than using Java. < /br>< /br> A nice mathematical library is SciPy. It gives a decent computer algebra system within python:
from scipy.stats import *
norm.rvs(scale=2.3)

from scipy import *
p=poly1d([1,2,5])
print p*p*p
A = mat('[1 2 3; 4 5 7; 4 1 2]')
b = mat('[1;2;6]')
linalg.inv(A)
linalg.solve(A,b)

A = random.rand(5,5)
print linalg.eigvals(A)
A comprehensive Python-based system for mathematics is SAGE by William Stein.

Unlike in Perl you can use it interactively: start by typing "python" and use it as a calculator.
Entering at the python prompt:
11234123412314L*1234123412341234L
pow(234L,23)
for example multiplies two integers (L is needed after the integer for long integers) and finds then 23423.
(1.5+9.5j)*(2.3+9.4j)  
returns the product of the two complex numbers, or
abs(2.23423+9.4j)
the absolute value of a complex number.
Most important mathematical functions are predefined. You might to load the math module before using them:
import math
math.sin(0.234234)
There are many libraries available. For example, to access random numbers, you use
import random
random.random()
An other library is the calendar:
import calendar
calendar.prcal(2001)
How to work with lists and dictionaries can be seen in invert.py. How to define functions in fibonnacci.py or ulam.py.
If a file like pi.py is made executable, then it can be called directly The program determines pi=3.14159.... to arbitrary many digits. You could also execute the program by directing in into python. You can also start up python, then type "import pi.py" at the python prompt. When looking at pi.py , you see the lack of parentheses. This is achieved by text structure. Unlike in other languages, it matters how the indentations are put.
To see an other example, look at factorize.py, when called for example as "./factorize.py 123123" it gives the factorization of the number 123123.
Python allows to write small programs very fast. For example quadratic.py, produces pseudorandom number generators using a quadratic iteration.
An example, of a python program which interacts with the user is the klingon.py game.
Python is however also used for large projects on the web. The webserver zope for example is written in python.

Links Books
  • Martin C. Brown, Python Annotated Archives, Osborne
  • Mark Lutz, David Ascher, Learning Python


Simplicity, Clarity, Generality B.W. Kernighan, R. Pike, in "The Practice of Programming".
Privacy Digital Accessibility
HTML CSS