The Python Programming Language

Copyright (C) 2009, All Rights Reserved
David Beazley
http://www.dabeaz.com

Presented at USENIX Technical Conference, June 14, 2009

Introduction

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

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

Organization - Part 1 : Functions

  • portvaluefunc.py. Portfolio value problem organized into functions.

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

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

Organization - Part 2 : Modules

Organization - Part 3 : Standard Libary Tour

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

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

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

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

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

  • helloserv.py. TCP server using sockets.

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

Organization - Part 4 : Creating New Kinds of Objects

  • stock.py. An example of a simple class.

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

  • madoffstock.py. A class that inherits from the Stock class.

  • thread.py. An example of defining a thread.

Organization - Part 6 : Some Encapsulation Tools

  • stock3.py. An example of a class with a property definition.

  • stock4.py. An example of a class with a property decorator.

  • descrip.py. An example of defining a descriptor.

Organization - Part 7 : Metaprogramming

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

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

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

  • makewrap.py. A function that creates a wrapper around another function.

  • decorate.py. An example of wrapping a function with a decorator.