Get Started With Python

The scripting language Python is named after the Monty Python British Comedy series, although the language takes after its reptilian namesake as well — it’s simple by design, yet flexible and powerful. It can’t exactly swallow large rodents whole, but it can be used for a wide variety of applications. Whether you need to automate some part of you desktop workflow, create a website or build a full-fledged desktop application, Python is a strong candidate for the job.

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.

Advantages

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.

Uses

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.

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:

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.

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]
's'
>>> x[:1]
's'
>>> x[1:]
'pam'

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.

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.

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.

Lists

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

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:

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.

Dictionaries

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

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.

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.

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
'monkey'
>>> s
'spam'

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
'b'

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:

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.

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.

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:

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.

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.

Conclusion

So now you know how to say “nee!” and perform some basic operations using Python. If you’d like some more practice head over to the Cheese Shop (we did warn you that the Monty Python puns were thick in the Python community), a site dedicated to Python scripts, and try downloading some things that look useful.

Next up we’ll take a look at how to write a script you can run from the command line and how to open and read data from a web service. In the mean time we’re off to return this parrot which seems almost like it’s, um, dead.