Quick Reference – Python



  1. What Is Python?
    1. Advantages
    2. Uses
  2. Installing Python
  3. Python Differences
    1. Spaces versus Tabs
  4. Getting Started
  5. More Strings, Formatting and Comments
  6. Lists, Dictionaries and Tuples
    1. Lists
    2. Dictionaries
    3. Tuples
  7. Assigning Multiple Values at Once
  8. The Python for Loop
  9. Suggested readings
  10. Conclusion

What Is 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. [source: python.org]

Python was created by Guido van Rossum, who now works for Google. As a result, the company uses Python extensively in its various web services, most recently App Engine. Even though we joked around about Python exhibiting some likeness to its slithering namesake, the language takes its name from Monty Python’s Flying Circus, which is why many Python tutorials feature shameless Monty Python puns. This one is no exception.



Simple Clean Syntax
Python has an extremely simple syntax. It has comparatively few keywords and well written code in Python tends to remain extremely readable years after it was written. Much has been made about using white space to denote code blocks in Python, however users of the language will note this adds to readability and standardizes code formatting across the Python community.

Object Oriented
Python has advantages over other object-oriented languages. In Python everything is object. In some object oriented languages like Java, almost everything is object, but there almost always seems to be an exception. This creates some rather clunky syntactical maneuvering. There is no such problem in Python. In Python, the so-called primitive types are objects, functions are objects and code blocks are objects — this allows for a more natural programming style.

No One Style Enforced
What if object orientation isn’t really your thing? Maybe you only write 50-line text processing scripts and have no need to write complex objects. Well, you will find Python perfectly suited for your needs. While Python has advanced object oriented design elements, it doesn’t force you to use them. You can write large programs in the procedural style in Python and never be bothered to learn the object oriented way of doing things. Python also supports some functional programming constructs like anonymous one-line functions (using the Lambda keyword) and cool features like list comprehension.

Standard Library
Python’s biggest advantage is perhaps its large all inclusive standard library. Unlike many other open-source languages, Python has almost everything you need to write almost any type of application included. So whether you are writing a database-backed web application or a cross platform networked console application, be sure that the standard library has you covered. If for some reason you need more than is provided by the standard library, there are many, many third party libraries out there. They cover everything from industry standard encryption libraries to 3D graphics frameworks.


Python is a compiled application that can be installed in a variety of environments — your desktop, a web server or embedded devices like your phone. Python can run on all the major platforms and comes pre-installed on Mac OS X and most Linux distros.

There are two main ways to use Python — either by starting it directly from the command line or by calling an external script. Obviously, the latter method is much more flexible unless you’re just doing a quick one-time program.

If you’re running Python on a web server, you can serve your scripts from a cgi-bin folder just as you would with a Perl script. There are also faster, more robust options like mod_python or mod_wsgi, two Apache modules that call on the Python interpreter to process your pages.

Before we dig in, you should know about a site called Python.org. Python is an open-source language, and Python.org is its control center, with extensive reference material, additional tutorials and a complete library of all the elements available in Python.

Installing Python

Python code can be written as scripts and saved in text files with the .py extension. There’s also a shell interpreter that makes it very easy to get started just by typing python into your shell prompt. For now, that’s what we’ll be using to show some of the basic language principles.

Go ahead and fire up a terminal window and type python. If you have Python installed, you’ll get a message like this:

Python 2.4.4 (#1, Oct 18 2006, 10:34:39)

[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin

Type "help", "copyright", "credits" or "license" for more information.


Tip: The three angle brackets are the standard Python command prompt which lets you know you’re now using the Python interpreter. Any time you see code snippets written with the >>> you know the author is referring to the Python shell.

If typing python in your shell didn’t load the interpreter, you’ll need to install Python. You can download the source from the Python website or use one of the many package installers available for most platforms.

As of this writing, the latest version of Python is 2.5.2. Mac OS X and most Linux distros ship with Python 2.5. Windows users may need to install the latest version.

Although Python 2.5 offers some nice new features over previous versions, for backwards compatibility reasons we’ll stick to concepts and tools that will work with Python 2.3 and greater.


Python Differences

For the most part, Python behaves much like PHP, Perl, Ruby and other languages you may be familiar with. However, there are some important and noteworthy differences.

Perhaps the most obvious (and Python aficionados would argue, important) is that line breaks and indentions in your code have meaning in Python. Whereas PHP and others use a semicolon or other mark to designate the end of a line, Python sees all new lines as, well, new lines.

Also where PHP and others use curly brackets to enclose code blocks, Python merely wants the code indented.

Python forces you to properly indent code blocks and eschews end-of-line punctuation like PHP’s semicolons in favor of simple line breaks.

This has some import consequences. First and foremost, it makes Python code much easier to read. The structure of a Python script is a snap to figure out at first glance. Even if you have no idea what the code is doing, you can tell how it does it just by glancing at it.

Python forces neat, well structured code, but it also forces you pay more attention to how you write your code. Consider the following two code snippets, which do very different things:

def myfunction():

    if x == y:

        return True

def myfunction():

    if x == y:

    return True

In the first code block our return statement is indented, and therefore within the if statement. In the second code block we didn’t indent the return statement so that function always returns true, regardless of our if test. Technically, that second function would generate an error because Python expects an indented block after the colon.


Spaces versus Tabs

As the joke goes, the most popular way to write Python code is to indent with spaces. The second most popular way to write Python is with tabs. Most good text editors have an “entab/detab” function which can convert tabs to spaces and vice versa.

The important thing is to be consistent. Don’t mix tab and space indenting in the same script! Doing so will cause Python to throw an error and your code won’t execute.

Getting Started

Assuming you’ve got Python installed, fire up a shell window and type python to start the interpreter. Here’s a simple script. Just type print 'hello world' and hit return.

>>> print 'hello world'

hello world


Any time you want feedback from Python, use the print statement. As with any language, Python has built-in tools for doing common things, like in this case, printing something out.

Let’s create some variable assignments and play around with some of Python’s built-in types. We’ll start by creating a string and playing around with it:

>>> x = 'spam'

>>> x[0]


>>> x[:1]


>>> x[1:]


The first line just assigns x the value of “spam.” Python is dynamically typed language, that is, there’s no need to tell Python that x is going to be a string, it will figure that out at run time.

It’s also worth noting that Python is a strongly typed language, which means that “types” are always enforced. If you try to treat x as a number after you’ve already assigned it a string value, Python will throw an error. In order to use x as a number, you’d have the explicitly recast x as a number first.

The next line x[0] shows how Python’s treats strings — much like a list (array) with each letter being like an element of the list. The x[:1] is an example of Python’s slicing methods. The basic syntax is variable[start:end]. Slice indices always start with 0 and by default, omitting first index defaults that value to zero, while omitting second index defaults to the size of the string/list.

Here are few more operators:

>>> x = 'spam'

>>> len(x)


>>> y = x

>>> x = 'knights'

>>> y



Note that last sequence, we set a new variable y to the value of x and then change the value of x, but y stays the same. In other words, y doesn’t become a reference to x, it’s a new variable with the value of x. Just because x changes doesn’t mean y will as well.


More Strings, Formatting and Comments

We already know that line endings and indentions matter in Python, but what about within Python strings? For instance:

>>> long_string = "Python is the best language ever, but I willn

... keep that to myself and not start flame wars on Slashdot.n

... Just becausen

...     I think it's the best doesn't mean everyone needs to"

>>> long_string

"Python is the best language ever, but I willnkeep that to myself and not start flame wars on Slashdot.nJust becausentI think it's the best doesn't mean everyone needs to"

>>> print long_string

Python is the best language ever, but I will

keep that to myself and not start flame wars on Slashdot.

Just because

        I think it's the best doesn't mean everyone needs to


So what’s going on here? How did we write multiple lines if line breaks mean the end of a line? The trick is the which tells Python to ignore the end of the line. Also note the difference between calling our variable directly and printing it using the print statement.

Escaping line breaks with a backslash also works within normal Python code blocks as well, not just within strings. In some cases it can help make your code more readable.

The other Python concept that might look a bit funny to those coming from other languages is the string formatting tools. In other languages the common way to add data to a string is to just concatenate the string like this:

"the beginning of a sentence" + variable_data + "the end of a sentence"

While this will work in Python as well, there is a far more elegant way to write it using the %s operator.

>>> b = 'beginning of a sentence'

>>> e = 'end of a sentence'

>>> v = 'variable data'

>>> '%s, %s, %s' %(b, v, e)

'beginning of a sentence, variable data, end of a sentence'

The other nice thing about using %s is that it will force the value to a string, whereas straight concatenation won’t.

Note: There is a similar %f for inserting numbers into a string.

One last thing about strings (aside from them being super-absorbant and perfect for doing away with floods and tidal waves).

To create comments in Python you have several options. Like Perl, the hash mark can be used for inline comments, like so:

x = 'spam' #initial value

The other way to comment your code is with doc-strings:

def superfunction(params):

   """ The super function can do things

       you've only dreamed of"""

   print 'Spam!'

A doc-string is an unassigned string at the beginning of a function definition. In this example, we’ve used a tripple-quoted string which can span several lines but a normal, single-quoted string is also OK.

A doc-string isn’t executed but is stored with the function to describe its purpose. Doc-strings can be accessed in your code – a feature which has been used to produce self-documenting and self-testing programs.

Lists, Dictionaries and Tuples

Arrays are one of the most useful constructs in a language. They allow you to store and manipulate compound data. Python has three distinct objects for handling compound data types: lists, dictionaries and tuples.


Perhaps the most useful is the list, which is pretty straightforward; it’s a list of data:

>> l = ['spam', 'ham', 314, 23]

>>> l

['spam', 'ham', 314, 23]

>>> l[0]


As you can see, we construct a list using square brackets and assign it to the variable l. From there, we can get the whole list by calling it directly or access individual list members using a zero-based index, just like we did earlier with the strings.

Also note that lists can mix together any data-type you want. Here, we used both numbers and strings, but you could add anything you want — even other lists.

Lists can do some other neat tricks. For instance, let’s try replacing some items:

>>> l[0] = 'monkey'

>>> l

['monkey', 'ham', 314, 23]

>>> # slightly more complex replace method:

>>> l[0:2] = ['banana', 18]

>>> l

['banana', 18, 314, 23]

>>> # we can even insert the list itself:

>>> l[:0] = l

>>> l

['banana', 18, 314, 23, 'banana', 18, 314, 23]

As you can see, lists are mutable. You can do pretty much anything you want with them. As you start getting more comfortable with Python, you’ll find yourself using lists all the time. Be sure to check out the Python docs for a complete list of list methods.


Another useful compound data-type is the dictionary. A Python dictionary is created using curly brackets, like so:

>>> d = {"monkey":"banana", "spam":"eggs"}

>>> d

{'monkey': 'banana', 'spam': 'eggs'}

The name:value pairs are the dictionaries keys and values. To access dictionary data you just call the key using the square bracket syntax like lists, but instead of a number you’ll use a key:

>>> d['monkey']


>>> #try to call something that doesn't exist and you'll get a KeyError.

>>> #this means that you can't get keys from values:

>>> d['banana']

Traceback (most recent call last):

  File "<stdin>", line 1, in ?

KeyError: 'monkey'

Like lists, dictionaries are mutable, which means you can add key:value pairs whenever you want. Just keep in mind two things: you can’t have duplicate keys, as the second will overwrite the first, and key names are case sensitive.

>>> #Start with an empty dictionary

>>> d = {}

>>> # add two key:value pairs

>>> d['monkey'] = 'banana'

>>> d['spam'] = 'eggs'

>>> d

{'monkey': 'banana', 'spam': 'eggs'}

>>> #try to add another monkey key

>>> d['monkey'] = 'wrench'

>>> # oops, we overwrote the original value:

>>> d

{'monkey': 'wrench', 'spam': 'eggs'}

As with lists, dictionaries can store any type of data you want. Even dictionary keys can be a variety of data-types, so long as the data-type is immutable (i.e. strings, integers, tuples, et cetera). And you can mix and match data-types within the same dictionary.

For more details on what sort of manipulations you can perform with dictionaries, check out the Python documentation page.


What the heck is a tuple? It’s got a funny name, but it’s just an immutable list. Once you stick data in a tuple you can’t change it. Unlike lists, tuples have no methods.

Tuples are created using parentheses, and data in a tuple can be accessed just as we did with a list. Often functions that operate on lists work on tuples, too.

>>> t = ('spam', 'ham', 314, 23)

>>> t

('spam', 'ham', 314, 23)

>>> t[0]


>>> t[0] = 'eggs'

Traceback (most recent call last):

  File "<stdin>", line 1, in ?

TypeError: object does not support item assignment

>>> len(t)


>>> max(t)


In many contexts, you don’t need the brackets to define a list. The comma alone is sufficient.

>>> t = 'spam', 'ham', 314, 23

>>> t

('spam', 'ham', 314, 23)

So you might be wondering, what’s the point of a tuple if it behaves like a crippled list? The answer is that tuples are faster than lists and Python uses them for holding some static sequences as we’ll see later. If you have a sequence of data that you know isn’t going to change, put it in a tuple.

Tuples are also handy for data that should not need to be changed, like in a private class method. Using a tuple instead of a list prevents outside tampering. Tuples can be keys in a dictionary but lists cannot.

Assigning Multiple Values at Once

One of the cool tricks in Python is ability to assign multiple variables values at the same time. For instance:

>>> m,s = 'monkey','spam'

>>> m


>>> s


But this gets much more powerful with more complex data. For instance we can also do this:

>>> a = 'b','c','d'

>>> x, y, z = a

>>> x


This is called ‘list unpacking’. Both sides of the assignment must have the same number of elements.

Another variation on this theme is the built-in range function. Consider this:

>>> range(7)

[0, 1, 2, 3, 4, 5, 6]

>>> Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday = range(7)

>>> Sunday


The Python for Loop

Of all the things that will have you scratching your head while learning Python, the for loop is probably the most obvious difference from other languages. Rather than iterating over a progression of numbers, or allowing you to define both the iteration step and halting condition (as in C), Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.

The best way to understand how it works is to look at an example. In this case, we’ll create a list and then loop through the values.

>>> a = 'spam', 'ham', 314, 23

>>> for b in a:

...     print b






Note that the variable b is totally arbitrary. You can name the iterator whatever you would like, preferably picking whatever makes the most sense. For instance, if your list holds a bunch of URLs, you might write for url in urls:.

We’d use the same technique to loop through a tuple, a string and other data-types. But what about a dictionary? That’s a little bit different, but we can borrow some of the multiple values techniques we mentioned above to help out.

>>> mydict = {'guido':'python','larry':'perl','rasmus':'php'}

>>> for key in mydict:

...     print key, 'created', mydict[key]


larry created perl

rasmus created php

guido created python

It’s worth noting that while Python’s for loops are just as capable as those in other language, the chances are that you won’t use them as much. One reason is that Python offers something called a list comprehension which does something similar but usually more elegantly .

For instance suppose you have sequence of numbers and you want to perform a multiplication function on them. You might be tempted to do something like this:

>>> new_list = []

>>> for num in 0, 1, 1, 2, 3, 5:

...     new_list += [num * 2]

>>> new_list

[0, 2, 2, 4, 6, 10]

But in Python, it’s much easier to just use a list comprehension. Here’s the same function in one line:

>>> [num * 2 for num in 0, 1, 1, 2, 3, 5]

[0, 2, 2, 4, 6, 10]

>>> # or if you want to store the results:

>>> new_li = [num * 2 for num in 0, 1, 1, 2, 3, 5]

>>> new_li

[0, 2, 2, 4, 6, 10]

It may take a while to wrap your head around, but list comprehensions are very very powerful and well worth spending some time with so you understand not just how they work, but when and where to use them.

Have a look at the official Python tutorial for more information.

Suggested readings

  • Learning Python, 3rd Edition, by Mark Lutz (O’Reilly)
  • Also worth reading through is Mark Pilgrim’s seminal Dive into Python which covers some of the same ground we went over here, but also hits on some other areas of interest like parsing XML with Python’s powerful DOM tools and attempting to demystify Unicode text handling in Python.


This site uses Akismet to reduce spam. Learn how your comment data is processed.