Wrap your mind around Python

Python is an open source scripting language that emphasizes speed and clarity in the code. The syntax is relatively simple, yet it's a powerful tool.

By Brent Doll

My first experience with Python was using it to customize Mailman, a mailing list management package. At first I was frustrated with the strict rules of syntax and the fact that white space mattered. But as I started to dig into the code, I found that the syntax made every Python script easy to read and understand. I now recommend Python along with my other favorite “P” scripting languages. It’s a powerful tool that should be in every open source developer’s arsenal.

What is Python?
Python is an open source scripting language emphasizing speed of development and clarity of code. It can be used for anything from simple scripting tasks to complex object-oriented application development. It is also considered to be an excellent language for beginning programmers because it's free, object-oriented, extensible, and enforces strict coding standards.

Python is free
Guido van Rossum began development of Python in 1990 as a pet project. Being a fan of Monty Python’s Flying Circus, he whimsically named his new programming language after the show. Python was originally designed to be an advanced scripting language to replace ABC for the Amoeba distributed operating system, but it soon grew to address a number of advanced programming problems and has now been ported to many platforms.

Guido is currently the chairman of the Python Software Foundation, which owns the intellectual property and licenses Python under the GNU Public License. Python is available for most major platforms, including Windows, Macintosh, and the popular UNIX variants. There are also developmental ports for PalmOS and Microsoft’s Pocket PC.

Python has advanced features
While Python can be used for creating simple scripts, it employs several advanced programming techniques, such as object-oriented code, sockets, threads, and GUIs. This is great if you're a beginning programmer because you can become familiar with syntax and coding practices and then learn other concepts that are incorporated in languages like Java or C++. Scripts can be executed stand-alone, but you can also make use of class files and variable object types to take advantage of the language’s true power.

Two things distinguish Python from popular object-oriented languages. First, Python emphasizes white space and coding structure, which make every developer’s code more reusable. Second, there is no need to compile Python code before executing it, which is why it is considered a scripting language.

Python is extensible
One great thing about Python is its ability to call out to libraries for functions that would be inefficient to accomplish within the language. Python has some useful tools to help produce complex applications. One of the more popular is Tkinker (pronounced "tinker"), which allows the development of cross-platform GUIs. Another tool, wxPython, is an extension to the wxWindows cross-platform C++ framework. WxPython currently supports the Windows and Linux platforms. PIL, the Python Imaging Library, enables Python to create, open, edit, and modify images, including .gif, .jpeg, and .png. To find popular Python extensions and applications, visit The Vaults of Parnassus.

Giving it even more leverage, Python also allows itself to be embedded into other programming languages. The most common of these is Jython, a Python interpreter written in Java. This allows you to leverage both languages’ advantages. You can use the clear syntax of Python in the powerful application framework of Java to create applications that depend on libraries from either language.

Coding standards
The stringent syntax of Python could be the top reason that new developers overlook this powerful language. Unlike with most other Web-oriented scripting languages, white space matters in Python, Instead of relying on brackets or semicolons to denote the end of a statement, new lines and tabs are used to delineate and create the visual structure of the code. This can be frustrating at first, but there is a hidden benefit—code readability. Python was structured this way based on the theory that a developer’s time is among the most valuable of an organization’s assets. Once you become comfortable with the peculiarities of writing Python code, it becomes second nature. It provides a built-in coding standard, which most Python developers rely upon.

Where to get Python
You can obtain a copy of Python for your favorite platform at ActiveState also has releases of the Python binaries for Windows, Linux, and Solaris.

The simplest of Python code
Let's take Python for a quick test drive so you can see it in action. Assuming that you are on Windows and you have installed Python, start IDLE, the Python GUI. You will be presented with the Python Shell. (If you are on an UNIX-like system, you'll need to type python to point to your Python executable.) Now, at the prompt (>>>), type:
>>>print “Hello World”

and the famous program words should appear. You can also assign variables simply by typing:
>>>myvar = 42
>>>print myvar

In this case, 42 should be printed out.

Python becomes more useful as you develop scripts that don’t need to be entered into the Python Shell. Listing A contains an example of the Hello World script with an if-else statement.

You can execute your script by typing (py being the standard extension for Python scripts) at the command line. You should see the desired text appear. The tab-indented statements in Listing A following the if and else are part of the white space formatting.

These very simple examples are provided to illustrate the clear syntax of Python. To learn more about programming Python, check out the Beginner’s Guide on the Python Web site. If Web pages aren’t your style, and you like to relax on the couch while reading, O’Reilly and New Riders have published some fine books on the subject. A couple of good ones to start with are How to Think Like a Computer Scientist : Learning with Python and Python Essential Reference.

Something for everyone
Whether you're a beginning programmer looking to start with a language that will allow you to grow your skills into more advanced concepts, or a Perl veteran who won’t mind losing some code structure control to gain greater readability of your code base, you'll find Python is a flexible and powerful language. It can develop anything from fully featured mailing list software to full-blown, cross-platform GUI applications.

Editor's Picks