Python Rocks! and other rants
Weblog of Kent S Johnson

2007-10-31 20:32:19

A Brief Tour of Python

This introduction is available online at /stories/00020.html.

Why Python?

The Python web site opens with this dense summary of Python:

Python is a dynamic object-oriented programming language that can be used for many kinds of software development. It offers strong support for integration with other languages and tools, comes with extensive standard libraries, and can be learned in a few days. Many Python programmers report substantial productivity gains and feel the language encourages the development of higher quality, more maintainable code.

Python is dynamic

  • Python has dynamic types. Types are not declared; instead, they are associated with values assigned to variables. Variables are essentially names for values.
  • Python has strong types. There are no automatic type conversions other than common numerical promotions; adding a string to an integer, for example, is an error.
  • Classes and functions can be created, inspected and modified at runtime. Classes and functions are first class objects which can themselves be saved, passed to and returned from functions, and modified on-the-fly.
  • The run-time environment is extraordinarily transparent and customizable. For example it is possible to inspect the call stack or modify the way object attributes are accessed.
  • Python includes automatic garbage collection - no need to worry about allocation and deallocation of memory.

Python is object-oriented

Object-oriented programming is a core capability of Python. Every object is an instance of a class, even "primitive" types such as numbers and strings. User-defined classes are easily created and used. The behaviour of class mechanisms can be changed by defining special methods and custom meta-classes.

Many kinds of software development

Python programs can be simple, straight-line scripts; they can be structured with functions; they can be object-oriented or functional; or they can mix all of these styles as appropriate. Larger programs can easily be broken up into modules and packages for organization and reuse. This makes Python scale well from small one-time scripts to large, complex projects. With the support of the standard library and third-party packages, Python is suitable for

  • scripting and automation
  • web clients and servers
  • GUI applications
  • database access
  • scientific and numeric programming

and almost any other programming task other than operating system components and real-time systems.

The Python web site also has an extensive list of applications for Python. The list of talks scheduled for Pycon 2008 gives a broad overview of some of the ways Python is used.

Integration with other languages and tools

Python integrates well with C and C++ and can call external libraries. Jython and IronPython are versions of Python that interoperate with Java and .NET.

Extensive standard libraries

Python comes with "batteries included". The Python standard library includes hundreds of useful, documented modules. I won't even attempt a list, see the Library Reference.

Note: Built-in functions and types are considered part of the standard library. They are documented in Section 2 and Section 3 of the Library Reference. These sections are well worth reading.

Can be learned in a few days

Experienced programmers can quickly become familiar with the language by reading the standard tutorial. Many beginners' resources for both new and experienced programmers are available online, as is the extensive documentation.

Two good books for experienced programmers who want to learn Python are Learning Python and Dive into Python. Dive into Python is also available online at A good book for new programmers is Python Programming for the absolute beginner.

The Python community is friendly, helpful and welcoming to beginners. The python-tutor mailing list (moderated by yours truly) is an excellent place to get help with beginner's questions. Some beginners say this list is Python's best feature!

comp.lang.python is another excellent resource, more advanced than python-tutor but still very newbie-friendly.

GNHLUG's own PySIG meets on the fourth Thursday of each month in Manchester, NH. Meetings generally include a question-and-answer session and one or two presentations plus milk and cookies.

Many Python programmers report substantial productivity gains

This is perhaps the most important reason to learn Python. Python is sometimes called "executable pseudo-code". Many programmers find that Python "fits your brain"; I often find that it is easier to write correct Python code to describe an algorithm than to write informal pseudo-code. Python gets out of the way and lets you quickly write the code you need. Python includes an interactive interpreter which is very useful for exploratory and ad hoc programming.

Python encourages the development of higher quality, more maintainable code

Python encourages writing very clean, readable code. It doesn't hide important details, nor does it require you to clutter your code with unneeded syntax. It gives you the tools to get a job done simply and clearly, then gets out of the way and lets you get to work.

Python is portable

Python is available for every major computing platform and many minor, obsolete and obscure ones. Well-written Python code is highly portable, often running on multiple platforms with no changes.

Easy + productive + powerful == Fun!

If you're not using Python, you're working too hard!

Getting Started

Python comes standard in many Linux distributions and Mac OS X and is sometimes included on Windows PCs. If you don't have it already, download the most recent release from or use the package manager for your Linux distribution.

Python programs can be created in any text editor. Many editors provide syntax highlighting, code completion and folding and other Python-specific features. If you plan to write many Python programs you will want to investigate this. An extensive list of editors with Python support can be found at

For a list of IDEs with Python support see

A Brief Tour

Python includes an interactive interpreter that is useful for exploration, learning and ad hoc programming. To start the interpreter, type python to a command prompt. You will see something like this (>>> is the interpreter prompt):

$ python
Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

Here is the canonical first program:

>>> print 'Hello, world!'
Hello, world!

The interpreter prints the result of evaluating the expressions you enter. This makes it useful as a calculator without any programming:

>>> 1+2

Built in types

Built-in types include integer, long integer, float, complex, string, unicode, list, tuple, set and dictionary. There is no character type; strings of length 1 represent characters.

Strings and numbers behave pretty much the way you might expect. Python supports long integers up to available memory:

>>> 'a' + 'b'
>>> 2**100

Strings are instances of str and support many operations including extracting substrings, formatting, simple search, comparison and case conversion. (Regular expression search is also available.)

The unicode type represents a Unicode string. Many codecs are included that translate between Unicode and byte-oriented character encodings.

Python values are strongly typed; the only automatic promotions are between numeric types. String multiplication by an integer has a special meaning but addition of a string and an integer is an error:

>>> 'a' * 3
>>> 'a' + 3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects


Lists are heterogeneous arrays implemented as arrays of object references. They can contain any kind of object including sublists or other structures. Lists support a variety of subscripts for reading and writing:

>>> a = [1, 2, 'b', ['c']]
>>> a
[1, 2, 'b', ['c']]
>>> a[0]
>>> a[2] = 'd'
>>> a[0:3]
[1, 2, 'd']
>>> a[-1]

Iteration over collections is supported directly by the language syntax:

>>> for item in a:
...   print item

Many built-in and user-defined objects implement the sequence protocol which allows them to be iterated with a for loop. Examples are lists, sets, dicts and files.

List comprehensions provide a convenient shorthand for operations that create a new list from a sequence:

>>> [ i*2 for i in a ]
[2, 4, 'dd', ['c', 'c']]


Tuples are immutable lists - they can be created, but once created their contents cannot be changed.

One use of tuples is for tuple assignment. If the left side of an expression is a tuple of names and the right side of the expression is a sequence with the same number of elements, the elements are assigned to the names. For example:

>>> x, y = 1, 2
>>> x
>>> x, y = y, x
>>> x


A dictionary (dict) is a mapping from keys to values. Dicts are implemented as hash tables. The keys can be any hashable object; typically they are numbers, strings, or tuples:

>>> d = {}
>>> d['Kent'] = 'Python'
>>> d['Scott'] = 'Java'
>>> d
{'Scott': 'Java', 'Kent': 'Python'}

The contents of a dict can be accessed by looking up a key, or as lists of keys, values, or (key, value) pairs:

>>> d['Kent']
>>> d.keys()
['Scott', 'Kent']
>>> d.values()
['Java', 'Python']
>>> d.items()
[('Scott', 'Java'), ('Kent', 'Python')]
>>> for k, v in d.items():
...   print '%s programs in %s' % (k, v)
Scott programs in Java
Kent programs in Python

Note the use of printf-like string formatting for output and tuple assignment to unpack the (key, value) pair into two variables.

Lists and dicts can be created easily with literals. This greatly facilitates data-driven programming in Python. For example, here is a lookup table that maps strings to lists of integers:

lookup = {
'a' : [1, 3, 5],
'b' : [2, 4, 10],

The ability to easily define complex nested structures is very handy.


Variables in Python are names for values, not containers for values. Programmers coming from C and similar languages will need a new mental model of variables and assignment. This can help: reset your brain.

Assignment creates a new name for a value, or binds a new value to an existing name. Assignment never copies a value, it just creates a new name for a value. This leads to aliasing which can confuse beginners:

>>> a2 = a
>>> a2 is a
>>> a
[1, 2, 'd', ['c']]
>>> a[0] = 5
>>> a
[5, 2, 'd', ['c']]
>>> a2
[5, 2, 'd', ['c']]

Control structures and blocks

Python supports if / elif / else and for and while loops with optional break and continue. Indentation defines the extent of a block; leading whitespace is significant. The required indentation is consistent with common formatting conventions in other languages. For example:

if a==3:
  b = 4
  b = 5

while a > 3:
  a -= 1

Functions and closures

Functions are defined with def. The type of the function parameters is not specified. The body of the function is an indented block:

>>> def square(x):
...   return x*x
>>> square(3)

It's easy to return multiple values from a function using tuple assignment:

>>> def return2():
...   return 1, 2
>>> x, y = return2()
>>> x
>>> y

Functions are first-class objects in Python. They can be assigned to variables, passed to other functions and stored in containers:

>>> def my_apply(f, x):
...   return f(x)
>>> my_apply(square, 4)

>>> d = { 'a' : square }
>>> d['a'](5)

Nested functions define closures. Here is a function that returns another function as its result; the returned function binds a parameter of the outer function in a closure:

>>> def make_adder(inc):
...   def adder(val):
...     return val+inc
...   return adder
>>> add5 = make_adder(5)
>>> add5(10)


Reading and writing files is straightforward. To read a whole text file:

f = open('myfile.txt')
data =

An open file is a sequence so it can be read a line at a time using a for loop:

f = open('myfile.txt')
for line in f:
  # process data in line

To write a file:

f = open('myfile.txt', 'w')

Reading or writing a given number of bytes and seeking to a location are also supported.

Modules and packages

Modules are Python libraries. Importing a module makes its contents available to the importing program:

>>> import sys
>>> sys.version
'2.5.1 (r251:54869, Apr 18 2007, 22:08:04) \n[GCC 4.0.1 (Apple Computer, Inc. build 5367)]'

Packages are collections of related modules. Packages and modules provide a structure for large programs and reusable libraries. The standard library is organized this way.

Object-oriented programming

Python fully supports object-oriented programming. Actually, every Python value is an instance of a class, even numbers and strings:

>>> 1.0.__class__
<type 'float'>

In Python the self parameter (equivalent to this in other OO languages) is always explicit. It is the first parameter to a method. Special methods whose names start and end with two underscores implement and modify standard behaviour of the class; __init__() is the name of the constructor. Here is a simple class with two methods. It partially implements a stack using a Python list as the underlying data store:

>>> class Stack:
...   def __init__(self):
... = []
...   def push(self, value):
>>> s=Stack()
>>> s.push(4)

Regular expressions

Perl-style regular expressions are implemented in the re module. Raw strings (with prefix r) simplify the creation of regular expressions containing backslashes:

>>> import re
>>> regex = r'id="([^"]*)"'
>>> m =, '<tag id="123" />')
>>> m
<_sre.SRE_Match object at 0xba160>

Easy introspection

Python objects are remarkably transparent. Introspection gives access to the attributes of every object. For example dir() shows interesting attributes of an object; getattr() and setattr() access attributes by name, and special attributes access the class of an object and the base classes of its class:

>>> dir(s)
['__doc__', '__init__', '__module__', 'data', 'push']
>>> getattr(s, 'data')
>>> s.__class__
<class __main__.stack at 0x89930>

Much of the runtime workings are accessible as well. The behaviour of classes can be customized with special methods and metaclasses; the runtime stack can be inspected.

The Zen of Python

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
© Kent S Johnson Creative Commons License

Comments about life, the universe and Python, from the imagination of Kent S Johnson.

Weblog home

All By Date

All By Category