The Python Programming Language

Copyright (C) 2009, All Rights Reserved
David Beazley

Presented at USENIX Technical Conference, June 14, 2009


This tutorial is an overview of the Python programming language. Although there are many facets of Python, this tutorial is most focused on scripting, and data manipulation.

Requirements and Support Data Files

This tutorial requires the use of Python 2.5 or newer. No third party modules are required. Examples have been tested on both Unix and Windows XP.

The following file contains some supporting data files that are used by the various code samples. Download this to your machine to work with the examples that appear in the tutorial.

This download also includes a PDF of the lecture slides.

Code Samples

Here are various code samples from the course. You can either cut and paste these from the browser or simply work with them directly in the "PythonProgramming" directory. The order in which files are listed follow the course material. These examples are written to run inside the "PythonProgramming" directory that gets created when you unzip the above file containing the support data.

Introduction - Part 2 : Python 101 - A First Program

Introduction - Part 3 : Basic Datatypes and File I/O

Introduction - Part 4 : List Processing

Introduction - Part 5 : Python Dictionaries

  • Compute the gain/loss of Dave's portfolio using dictionaries.

Organization - Part 1 : Functions

  • Portfolio value problem organized into functions.

  • Example of a generator function that follows a log-file like 'tail -f'. Run the program in the background to obtain a simulated 'access-log' file as might be produced by a web server.

  • Example of a coroutine function and a generator function used together.

Organization - Part 2 : Modules

Organization - Part 3 : Standard Libary Tour

  • Find the 10-largest Python source files in a directory tree.

  • Example of using the subprocess module to run another program.

  • Simple examples showing the use of the regular expression (re) library.

  • Example of using urllib to submit a form to a website. Bus arrival times for the CTA.

  • Example of submitting a form and parsing XML with ElementTree.

  • TCP server using sockets.

  • A web server that delivers files out of this directory. Connect at http://localhost:8080.

Organization - Part 4 : Creating New Kinds of Objects

  • An example of a simple class.

  • A class that overrides the __repr__() method to change printing.

  • A class that inherits from the Stock class.

  • An example of defining a thread.

Organization - Part 6 : Some Encapsulation Tools

  • An example of a class with a property definition.

  • An example of a class with a property decorator.

  • An example of defining a descriptor.

Organization - Part 7 : Metaprogramming

  • An example of a function that accepts another function as input and calls it.

  • An example of a function that accepts another function as input and calls it with any set of possible input arguments.

  • A function that creates a new function and returns it (also known as a closure).

  • A function that creates a wrapper around another function.

  • An example of wrapping a function with a decorator.