Introduction to Python for Math at UMBC


Contents

  1. Installing or Getting To Python at UMBC
    1. Using Python on the Web
  2. General Use of Python
    1. The Basics
    2. Help on Commands
    3. Editing Command Lines
    4. Running Commands in a File
  3. Number Theory Commands
  4. Linear Algebra Commands
  5. Probability & Statistics Commands
  1. Libraries

[Prev] [Python Intro] [Next]

Python is a general purpose interpreted language which has several major advantages for math and science users. Python is freely available, is pre-installed on most UNIX/OSX/Linux systems, and has features (packages, operator overloading) which lend themselves to the mathematics user. It is an interpreted language, so it can be used like a calculator for simple computations.

Installing or Getting To Python at UMBC

Python is free and can be easily installed on most computer systems. Downloads are available from the Python home page [http://www.python.org]. Python is pre-installed on UNIX/OSX/Linux systems. It is thus available in computer labs, on remotely accessible servers and on home machines.

The classic way to use Python is as a command line application.

Using Python on the Web

Recently there have been ways made available to write and run simple examples of Python code from within a web page.

General Use of Python

The Basics

At the Python command prompt (which usually looks something like >>>) type the command to be run, and then press the Return key to have Python run the command. As a simple example, if you want to add two integers type:

3 + 12

Intermediate values can be saved by giving them a name and assigning them a value with the = operator. If the following two commands are run the second output will be the value 4:

thesum = 3 + 15
thesum - 14

Python is very careful about exact integer values. Thus the quotient 12/5 returns a value of 2. If a decimal (aka floating point) value is wanted one of the operands can be written in that form, so 12.0/5 returns a value of 2.3999 (which is approximately 2.4).

When you are done with Python, hold down the Control key and type d.

Help on Python

The most authoritative and up-to date source for Python documentation is on the Python home page at [http://docs.python.org/]. Particularly useful for the beginner are the various tutorials listed at [http://wiki.python.org/moin/BeginnersGuide]. The basic Python commands are documented at [http://docs.python.org/library/functions.html].

Other references include:

Editing Command Lines

When working with Python you can move in a line using the usual left and right arrow keys. Earlier commands can be recalled with the up arrow key. If you are familiar with the simple Emacs commands you can use them as well.

Running Commands in a File

If a file contains python commands they can be executed either from the UNIX command line or from within a Python session:

A more flexible way to import Python commands into a session is with the import command.



Libraries

A small collection of my warranty-free, freely-usable Python math libraries.


[Prev] [Python Intro] [Next]
Robert Campbell
29 May, 2014