David Abel

Portrait


Python Tutorial

python logo

This tutorial is intended as an introduction to Python for folks with some prior programming experience.

Section 5: Modules (no file)

There are loads of useful modules in python. Here we will address a few of the modules I use regularly that I think it's good to know about.

One of the most helpful commands in python is __doc__, which allows you to access the doc string for any object, function, class, module, or method that has a doc string (a detailed comment explaining what the object/class/method is/does). This is especially helpful when dealing with large code bases or modules that you are unfamiliar with.

For instance:

>>> import math
>>> print math.__doc__
This module is always available. It provides access to the mathematical functions defined by the C standard.
>>> print math.pow.__doc__
pow(x, y)
Return x**y (x to the power of y).

Another way of getting useful information is the help() command. Calling help on a module, class, or function, brings up the man page for that object.

Here are some of my most used modules:

  1. math: The math module contains most the mathematical operations for python. Some highlights include:
  2. random: The random module is used for all operations related to random number generation. It has functionality to randomly shuffle lists, generate random lists, and generate random values of a variety of data types.
  3. numpy: The numpy module is one of the real work horses of scientific programming in python. It is equipped with an arbitrary dimensional array, as well as a variety of linear algebra operations.
  4. pickle: For serializing data.
  5. pygame: For game development (and some nice GUI functionality)
  6. matplotlib: Basic plots
  7. PIL: Python Imaging Library. Basic functionality for opening and manipulating images. For more advanced techniques, check out SimpleCV and OpenCV.
  8. SciPy: Lots of nice stuff for handling scientific computing
  9. ArgParser: For handling command line arguments
  10. os: For basic operating system functionality (opening files, checking if directories are empty, etc.).

numpy

Lets talk about numpy for a bit, since it's one of the most used modules for scientific computing with python. The arrays provided by numpy are extremely powerful, yet relatively easy to create and manipulate:

>>> import numpy as np
>>> arr = np.arange(6)
>>> print arr
[0 1 2 3 4 5]
>>> arr = arr.reshape(2,3)
>>> print arr
[[0 1 2]
[3 4 5]]
>>> print arr.ndim
2

You can also quickly make matrices of common values quickly:

>>> print zeros((2,3))
[[ 0. 0. 0.]
[ 0. 0. 0.]]
>>> print ones((2,2,3))
[[[ 1. 1. 1.]
[ 1. 1. 1.]]
[[ 1. 1. 1.]
[ 1. 1. 1.]]]

The arange() function is similar to python's range() function, but it can be used to create numpy arrays:

>>> print arange(0,50,5).reshape(2,5)
[[ 0 5 10 15 20]
[25 30 35 40 45]]

The randn() function in the numpy.random module creates an array of the specified dimenisions with random floats between 0 and 1:

>>> from numpy.random import *
>>> print randn(3,4)
[[-0.3157525 1.04280707 1.01558404 -0.53489289]
[-0.51217785 0.32228728 -0.74847843 -0.70625552]
[ 0.00748319 -0.71369062 0.98813116 -0.08820002]]

< Previous Section Next Section >