Using Python in Mathematics
Students in mathematics, science, and engineering need to develop some sort of computer literacy to accompany their work in their area of interest. There is no denying that many current developments in mathematics, science, and engineering are driven by modern software written to facilitate study in each of these areas.
In these pages we will explore the use of Python, which is seeing rapid development as an experimental tool for numerical studies and simulation in mathematics, science, and engineering. We will begin with some advice on installation and configuration, then the links on the right-hand side of this page will lead to activities designed to teach the reader how to use Python to asssist in their studies in mathematics, science, and engineering. Be sure to come back and visit this page often, looking to see what new links to activities have been created since your last visit.
Installation
-
Windows Users: Visit Python.org for all things Python. From there, visit the Download Page. Most windows users will want to download and install this version of Python. Once you have installed Python, the you will also need several packages to configure a Python environment that will let you get started with scientific computing.
- We will first visit Scipy.org to download binary distributions of Scipy and Numpy, Python packages that contain libraries for handling matrices and valuable mathematical routines. On the Numpy and Scipy Download Page follow these links to download binaries for Numpy and Scipy:
- The next utility provides the user with a Matlab-like environment for plotting. Visit the Matplotlib Site for an overview of this product. The download link on this page, if followed carefully, will eventually lead to the Sourceforge site where installations are available for various platforms. The following link leads to a binary distribution compatible with the above installations of Python, Numpy, and Scipy.
- Downlaod Matplotlib
-
Macinstosh Users: My Mac comes with Python already installed, but it is an extremely old version and updating to the latest version is required. The following instructions will install Python in
/Library/Frameworks/Python.framework/
and will not interfere with the preinstalled version that lies in:
/System/Library/Frameworks/Python.framework/
The new version and the old version will coexist harmoniously. The installation process will also set up your system to run the newer version.
Important! You should never delete the installation in the /System folder. You can however, uninstall the newer version (should you want to start over) by simply deleting the entire folder /Library/Frameworks/Python.framework. That is, delete the folder Python.framework in the folder /Library/Frameworks. Don't delete the folder /Library or /Library/Frameworks/ unless you feel a strong need for reinstalling your operating system.
Before you begin, you must have Apple's Developer Tools installed. If they are not installed, you can install the X11 tools from your Mac OS X installation disk by selecting customize and checking the X11 tools.
- Download the file python-2.5.2-macosx.dmg. Double click the file python-2.5.2-macosx.dmg to mount the file python-2.5.2-macosx.dmg in the finder, then double-click and install the file MacPython.mpkg. You will need your password to complete the installation process. Test your installation by starting Python as shown below. Then enter the command print 'hello world'. If Python responds as shown, you are good to go! Quit Python with the command quit().
MathDept $ python Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53) [GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print 'hello world' hello world >>> quit()
- Once the basic installation of Python is complete, visit the page:
-
http://peak.telecommunity.com/DevCenter/EasyInstall
This page contains instructions on using the program easy_install to install, update, and maintain Python packages. You will first want to download the following file into a temporary directory or folder on your system:
Open a terminal window, change to the directory containing the file ez_setup.py, then issue the following command:
sudo python ez_setup.py
You will need to supply your password when prompted.
- It is now a simple matter to install NumPy. Simply issue the following command:
sudo easy_install numpy
Test your installation by starting python and issusing the commands that follow:
MathDept $ python Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53) [GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import numpy >>> numpy.test(1,10)
Quit Python with the command quit().
- Next, install SciPy. Simply issue the following command:
sudo easy_install scipy
Any complaints about a fortran compiler can be alleviated by installing
http://r.research.att.com/gfortran-4.2.1.dmg.
However, if you've installed the X11 developer tools from your disk, this should not be necessary.
Test your installation by starting python and issuing the commands that follow:
MathDept $ python Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53) [GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import scipy >>> scipy.test(1,10)
- Install Matplotlib with this simple command:
sudo easy_install matplotlib
Test your installation with these commands:
MathDept $ python Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53) [GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from pylab import * >>> x=arange(0,2*pi,0.1) >>> y=sin(x) >>> plot(x,y) >>> show()
These commands should result in the following figure:

Figure 1. A plot of y = sin x on the interval [0, 2π].
- Install IPython as follows:
sudo easy_install IPython
Test the installation as follows:
darnold $ ipython Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53) Type "copyright", "credits" or "license" for more information. IPython 0.8.2 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object'. ?object also works, ?? prints more. In [1]:Type quit() or Ctrl + d to exit IPython.
That should do it! You should now have a working Python environment for scientific computation. In the activities, we will show you how to use these installed tools.
- Download the file python-2.5.2-macosx.dmg. Double click the file python-2.5.2-macosx.dmg to mount the file python-2.5.2-macosx.dmg in the finder, then double-click and install the file MacPython.mpkg. You will need your password to complete the installation process. Test your installation by starting Python as shown below. Then enter the command print 'hello world'. If Python responds as shown, you are good to go! Quit Python with the command quit().
