Introduction to Python

Ilan Schnell

December 19, 2006 - Madison, WI

What is Python?

Python is an interpreted, interactive language.

Official site:

To get started read the tutorial by Guido van Rossum (creator of the Python programming language).


Comes with many modules for: XML parsing, operating on tree objects, compression, network programming, regular expressions, URL parsing, hash functions, ...

Since python is interpreted it is slow, however critical parts of a program can be implemented in C, using SWIG if you want.

Python is pre-installed on many Linux distributions, however python is platform independent.

Installation (as usual): ./configure; make; su; make install

My examples have been tested with Python 2.5 (current version).

From the command line:

$ python -c "print 'Hello World!'"

Create a file

print 'Hello World!'

and then (from command line):

$ python

Add at the top of your file: (and make it executable)


and then (from command line):

$ ./

Interactive python shell:

$ python

now you can enter any python code:

>>> print 'Hello World!'
Hello World!
>>> (352+817)*781

Most important Built-in Types

Python a is dynamically typed language, which means that values have types (not variables).

>>> x=3.5
>>> type(x)
<type 'float'>
>>> x='Hello'
>>> type(x)
<type 'str'>
>>> x=[ 37, 'a', 1.24, True, [12, 23], {'a':3,'b':4} ]
>>> type(x)
<type 'list'>
>>> type(10**100)
<type 'long'>

Interactive (example) session:

>>> p='the five boxing wizards jump quickly'
>>> p
'the five boxing wizards jump quickly'
>>> len(p)
>>> len(set(p))
>>> p.count(' ')
>>> words = p.split()
>>> words
['the', 'five', 'boxing', 'wizards', 'jump', 'quickly']
>>> len(words)
>>> words[3]
>>> words[:3]
['the', 'five', 'boxing']
>>> '--'.join(words)
>>> p.replace(' ','--')
>>> p.find('wiz')
>>> p.find('fox')

Simple File Parsing:

Suppose we want parse the /etc/passwd file,

sshd:x:71:65:SSH daemon:/var/lib/sshd:/bin/false
ilan:x:500:100:Ilan Schnell:/home/ilan:/bin/tcsh

and create a dictionary of the uid mapping to the username.


for line in file('/etc/passwd'):
    lst = line.split(':')
    uidmap[lst[2]] = lst[0]

while True:
    print uidmap[raw_input('Please enter uid: ')]

Program in action:

$ ./ 
Please enter uid: 500
Please enter uid: 71

Whitespace is significant

if 1+2==3:
  print 'Hello'
  if True:
       print x+1.2
   msg='Not true'
   print msg

Python forces you to use the indentation that you would use anyway.

Python does not allow to obfuscate the structure of a program by using bogus indentations.

Have you ever seen code like this in C or C++?

/*  Warning: obfuscated C code! */
if( i == j )
    if( k == 0 )
        B[i][i] = 1.0 / A[i][i];
    B[i][j] = 4*k;

Handling Exceptions

It is possible to write programs that handle exceptions.

import sys

def usage():
    print 'Usage: %s <file>' % sys.argv[0]

if len(sys.argv) != 2:

filename = sys.argv[1]

    f = open(filename)
    print 'Opening `%s` for reading...' % filename
    print 'Could not open `%s` for reading.' % filename
    print 'Using default file instead.'
        f = open('default.txt')
        print 'Could not open `default.txt` either.  Exiting'


Importing modules

The many modules can easily be imported.

>>> import math
>>> math.sqrt(2.0)

Import all objects from a module into current scope:

>>> from math import *
>>> sqrt(2.0)*cos(pi/4)

Only import certain objects from a module:

>>> from math import exp, pi
>>> exp(pi)
>>> cos(pi)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'cos' is not defined


>>> from math import pi, sqrt as rt
>>> 1/rt(2*pi)


Lets take a look at a simple function:

>>> def sum(x, y):
...     return x+y
>>> sum(5, 3)
>>> sum('a', 'bc')
>>> sum('a', 5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in sum
TypeError: cannot concatenate 'str' and 'int' objects

What if the function should handle this case as well?

>>> from types import *
>>> def sum(x, y):
...     if type(x)==StringType and type(y)==IntType:
...         return x+str(y)
...     return x+y
>>> sum('a', 5)
>>> sum('Hello ', 'World!')
'Hello World!'
>>> sum(1100, 325)

Regular Expressions

The re module and provides Perl-style regular expression patterns.

For an introduction, I recommend A.M. Kuchling's howto.

Say we wish to have a program that tells us whether an email address (in its first argument) is syntactically valid.

import sys, re

pattern = re.compile(r'^[\w.-]+@[\w.-]+\.[A-Z]{2,4}$', re.I)

if pattern.match(sys.argv[1]):
    print "OK"
    print "Something is wrong with this email address."

Here is how you invoke the program:

$ ./
$ ./
Something is wrong with this email address.

Checking Examples in Source Code

The doctest module allows you to check examples in your source code.

Consider the following code

def fib(n):
    """Returns Fibonacci series up to n:
    >>> fib(100)
    [1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
    res, a, b = [], 1, 1
    while b <= n:
        a, b = b, a+b
    return res

if __name__ == "__main__":
    import doctest

When you run

$ python

the example inside the source code will be verified.

>>> from fib import *
>>> fib(100)
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

Basic XML parsing:

Print the values of src values for all img elements found in a well-formed XHTML document retrieved from the internet.

from urllib2 import urlopen
from xml.dom.minidom import parse

dom = parse(urlopen(''))

for elt in dom.getElementsByTagName('img'):
    print elt.getAttribute('src')



The exec statement

Executes a string of a python code.

Suppose you which to write a program which reads a config-file ~/.myprogrc:

# Configuration file for my progamm
verbose  = True
errorlog = "/var/log/myprogram/error.log"
colors   = { "fore" : "#ae2020",
             "back" : "#f7d8c2" }

Instead of scanning, parsing and analyzing the config-file yourself, write the config-file in python syntax and let python do all the work for you:

import os.path
if verbose:
    print 'Welcome to my program!'
    print 'Background-color is:', colors['back']

Running the program in a shell:

$ ./
Welcome to my program!
Background-color is: #f7d8c2

Network programming in python

Simple HTTP Server

What our server does?

Browser when connecting to our server:

from SocketServer import BaseRequestHandler, TCPServer
import sys

if len(sys.argv) != 2:
    print 'USAGE: %s <port>' % sys.argv[0]

def htmlSave(s):
    htmlentList = [ ('&','&amp;'), ('<','&lt;'), ('>','&gt;') ]
    return reduce( lambda c, (a,b): c.replace(a,b), htmlentList, s )

class RequestHandler(BaseRequestHandler):
    def handle(self):
        print "Client connected:", self.client_address
        content="""<html><head><title>HTTP Server</title></head><body>
<h2>Information about this HTTP/TCP request:</h2>
<p>IP: <b>%s</b><br />
 Port: <b>%s</b></p>
<pre style='border: solid 1px #bdf; background-color: #def;'>%s</pre>
""" % ( self.client_address[0],
        htmlSave(self.request.recv(2**16).replace('\r','<CR>')) )
        self.request.sendall( """HTTP/1.0 200 OK\r
Content-Type: text/html; charset=utf-8\r
Content-Length: %s\r
\r\n%s""" % ( len(content), content ))

TCPServer(('',int(sys.argv[1])), RequestHandler).serve_forever()


Generators are a special class of functions. Regular functions compute an object and return it, but generators return an stream of objects.

def fib():
    a, b = 1, 1
    while True:
        a, b = b, a+b
        yield a

The above function will return an infinite stream of Fibonacci numbers:

>>> i = fib()
>>> for n in i:
...     print n,
8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657
46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887
9227465 14930352 24157817 39088169 63245986 102334155 ...

More on functions

In python functions are ordinary objects. Therefore, a function can take functions as input, or return new functions, or both.

The function integral takes another function as an argument:

def integral(f, a, b):
    N = 1000
    dx = (b-a)/N
    return sum(f(a+dx*n) for n in xrange(N))*dx

The function mkAdder takes an integer an returns a function:

def mkAdder(n):
    def add(i):
        return i+n
    return add

add3 = mkAdder(3)
print add3(2)       #   5
print add3(8)       #  11

print mkAdder(5)(4) #   9

The last line may seem peculiar in that a single function (+) of two arguments (the two numbers being added) is written in terms of two functions which take one argument each.

Higher order functions

A higher-order function takes functions as input and returns new functions.

def compliment(f):
    Returns the compliment (which is a function) of function f.
    def res(*args):
        return not f(*args)
    return res

def even(n):
    return bool((n)%2 == 0)

odd = compliment(even)

Note that the argument of compliment is not limited to a function which only takes one argument.

Without the higher-order function compliment, we would have to write:

def odd(n):
    return not even(n)