Python 101

by Evelyn Mitchell
Wednesday, 3rd August 2005

The Other Scripting Language that Starts with "P"

Columnist Evelyn Mitchell gives an introduction to Python, a unique, open-source scripting language popular with many Linux coders. Once you have glimpsed the power and simplicity of Python, you may never go back. Evelyn compares Python with Perl, Java, and Tcl, illustrating differences along the way with explicit code segments.

Python is the other scripting language that starts with "P". It was named for Monty Python, the comedy troupe, and references to their skits and films (most commonly to "Spam") appear frequently in Python programs. Highly popular with Linux users and in the open source community in general, Python has a number of advantages over other languages like Java and Perl, some of which we will cover in this article.

Let's jump right in with the unavoidable and always instructive Hello World example, as a springboard into the discussion:

#!/usr/bin/env python
print "Hello World"

produces the result:

Hello World

For the sake of comparison, here's the same program in Perl:

#!/usr/bin/perl
print "Hello World\n";

You probably noticed a couple of things about the Python code right away. Consider the command:

!/usr/bin/env python

Portability is one of Python's strong points. Using this #! line rather than the actual location of python on your system allows you to move this code, without modification, to any system that has /usr/bin/env (the equivalent #! line for Perl points directly at the Perl binary).The result is that you can run Python code on more platforms than Java. You can even run Python code within a Java Virtual Machine, using JPython (see Resources).

Plus, because Python is an open-source language, it is not subject to the same sort of OS-specific tweaking that Java is known for. (Actually, there are some "OS-specific" extensions to Python -- and not just for Microsoft operating systems. There are SGI-specific sound libraries, for example. These extensions notwithstanding, it is still quite easy to write OS-agnostic programs under Python.)

Python is a scripting language, so you don't have to define constants or variables before you use them. This can speed code creation when you're trying out alternative solutions to a problem for a prototype.

What's more, as the Hello World example illustrates, Python doesn't use a lot of punctuation. In particular, Python doesn't use the semicolon (;) to mark the end of line, eliminating a whole class of errors that I tend to make in languages like Perl due to mistyping a character.

You'll also notice that you didn't have to write "Hello World\n", as in Perl. This is because the print statement in Python is smart enough to assume that you usually want a newline after your prints. If you don't want a newline, just add a comma at the end. The comma operator inserts a space:

print "hello", "world"

produces the result:

Hello World

The example below concatenates the two strings without a space in between:

print "Hello" + "World"

produces the result:

HelloWorld

Python and Perl

If Perl is the first post-modern programming language as Larry Wall (Perl's creator) describes it, then Python can be called the first neo-classical programming language.

Both Python and Perl build on a strong understanding of the available tools for problem solving. Larry Wall used awk, sed, and shell scripting as the primary design inspirations when he started writing Perl because he was writing a language to make system administration tasks easier. Python was inspired more by object-oriented design and object-oriented tools.

Rather than striving for a maximally expressive language as Larry Wall did, Guido van Rossum (the designer of Python) chose to create a simple, powerful, elegant base for creating systems out of components. The expressive power is still there in Python, but it tends to be compartmentalized, which makes reading Python code much easier. When you need more detail on how a certain line of code works, you look at the modules and functions it is using, rather than see the full complexity on the surface.  

Python and Tcl

Tcl on the surface looks a lot like Python. Both languages can be used to write elegant, simple code. Tcl is primarily a string processing language. The only datatypes in Tcl are strings and one-dimensional arrays of strings.

Python, in contrast, comes with a full range of data types including numbers (integers, long integers, floating point, octal, hex, and complex numbers), strings, lists, dictionaries (arrays indexed by keys, like hashes in Perl), and tuples (simple lists that can't be changed in place).

Tcl and Python share the same GUI toolkit, Tk. Python's interface to Tk is called Tkinter, and it is included with all recent versions of Python. You are not limited to using just Tk to build GUIs in Python, however. You can also use the Gimp Tool Kit (GTK) and Gnome extensions to GTK+.

Python and Java

Java is a full-featured programming language with a complete range of types, thread support, strong typing, and all of the other features you expect.

Python is a scripting language. It doesn't use or offer strong typing, which is nice for prototyping but can cause problems in more complex implementations. It does offer thread support, but it isn't as rugged as Java's thread support.

Installing Python on Linux

If you are running a Red Hat Linux system that uses RPM, installing Python is as simple as downloading the RPM from the Python.org Web site (see Resources) and typing:

rpm -i python-1.5.2-2.i386.rpm

Red Hat 6.0 shipped with Python 1.5.1, so you will need to update it if you have it already installed:

rpm -u python-1.5.2-2.i386.rpm

If you are running Debian Linux, you can download Python and some additional modules from the standard Debian distribution using apt/dselect.

Other Linux platforms will need to compile Python from source. To do this follow the instructions in the README file in the distribution, or consult your local expert.

But you don't need to choose between Java and Python. Using JPython (see Resources), you can write Python code that is evaluated within a Java Virtual Machine. You don't even have to take a performance penalty. In fact, there have been several reports of a marked speed increase between regular C Python and JPython because of some optimizations in the implementation of dynamic datatypes in the JPython port of Python.

Why is Python popular with Linux users?

There are several reasons why Python is gaining in popularity with Linux users. Python users don't match the sheer numbers of Perl users, but users who have tried Python tend to continue using it for these simple reasons:

What is Python Bad For?

Python is not the perfect language for every programming task. For example, it is not suited to system-level programs, like device drivers and kernels, because it is too high level to give tight control over memory allocation and other low-level tasks. Also, because it is relatively slower than C and other compiled languages, Python is not well suited to computationally intensive applications, though it can be used as a framework or glue language around such applications.

Who uses Python?

The most direct example of a Linux Python user is Red Hat. They use Python extensively for their system administration tools and configuration tools.

Other companies using Python include DigitalCreations, who received venture capital on the condition that they released the source to their Web authoring system, Zope, which is written in Python, and which uses Python as the extension scripting language.

Notable programmers who have joined the Python camp include Eric Raymond, who has chosen Python as the implementation language for his Trove project, and Bruce Eckel, author of Thinking in Java , who says "Python has the potential of being an exceptionally productive language".

How to get Python

Most Linux distributions ship with a relatively recent version of Python. If you're interested in getting the most up-to-date version, the Python.org Web site (see Resources) has binaries for most popular operating systems, as well as source and RPM distributions.


For the last five years, Evelyn Mitchell has concentrated on Web development in Perl, PHP, Python and, most recently, Zope. In the financial industry, she has programmed for online trading Web sites. Currently as managing partner of tummy.com, she is responsible for project leadership and direct supervision of developers and admins. She knows firsthand that open source development makes for better software and for better programmers. She can be reached at efm@tummy.com.
View the original article Python 101