Getting started with Python

Easy to learn, easy to use: Python is worth checking out.

By Cameron Laird and Kathryn Soraiz

February  1998
[Next story]
[Table of Contents]
Sun's Site

Do you work in C, C++, or Java on large projects? We'll show how Python will give you results quicker and more reliably. Are you already scripting in a language other than Python? You'll want to see how Python's careful engineering offers an easy-to-use, interactive, clean, object-oriented, extensible, scalable language. Whatever programming you're doing now, Python can improve it. (3,000 words)

Mail this
article to
a friend

Tell me more about Python.

That was the most common request from readers of our recent story on scripting languages. And the readers are right. Python deserves more attention.

Python, like BASIC, originated in an educational environment. Guido van Rossum created Python over the 1989/1990 winter holidays while working as a researcher in Amsterdam. In the 1980s he had co-authored the second implementation of a scripting language, ABC, to teach computer concepts. ABC never gained the popularity he felt it merited. Van Rossum reworked it in early 1990, joining ABC's elegance and ease of learning with deliberate openness to "foreign" components. He also removed a few idiosyncrasies that distracted experienced users and boosted performance. The result was Python (named, by the way, after the British comedy troup, Monty Python).

A language without hang-ups
Python is a remarkable synthesis. It handles large object-oriented programming tasks with greater ease than systems languages such as C, C++, or Java. And, because it is a scripting language, learning Python is a snap. Unlike C or C++, written to make life easier for the computer, Python was designed to be easy on the programmer.

Python is a pure object-oriented systems development language. This is unusual for scripting languages because they generally include object orientation only as an afterthought, and are at their best in small projects. Python, in contrast, scales well. Its scoping rules and clean design make it readable even amongst multiprogrammer teams, and its package and module facilities effectively structure big jobs. Python is a general-purpose language that is also a good prototyping language.

Python incorporates the best of scripting and systems languages

Like such other modern, freely-available scripting technologies as Perl, Scheme, and Tcl, Python is versatile. You can:

And you can do all this from most operating systems (including Amiga, MacOS, MSDOS, OS/2, Unix, VMS, and Windows).

What's the best part of Python? It excels at team projects. We think of Python first for any job that involves several people. There are several reasons:

In short, even though it's a scripting language, Python competes with conventional programming languages and deserves serious consideration by anyone who needs the strength of a conventional compiled language for a workgroup-sized job.


Starting off right
Let's get Python working so you can see these benefits for yourself. With a copy of the interpreter on your desktop you have the opportunity to practice Python as you read along.

Setting up Python is easier than the typical installation of a Java or C compiler. Even with a slow connection, it takes less than an hour to retrieve an installable form of a fully functioning interpreter from the binary repository (see Resources below) and install your own copy of Python.

When you launch the interpreter (either by clicking on an executable icon or invoking "python" at the command line) you'll see a ">>>" prompt. In a pinch, you could almost teach yourself Python by playing within the interpreter. The syntax and keywords will be familiar: print "Hello, World." and a = 7; print 9 * a do just what you expect. The distribution includes an extensive and well-written reference manual. However, if you're new to Python, you're best off with one of the fine books or online tutorials that you'll find in listed at the Python home page.

For system administration, we need a utility to compare a source tree to an archive from which it had been modified. This is the traditional province in Unix environments of shell or Perl programming. Python's object orientation and metaprogramming (programs that manipulate programs) don't matter in this simple case. However, what does demonstrate is Python's readability. This was one of our first efforts in Python:


from sys import *
from os import *
from posix import *

# Define a function which "roots" a path reference, if
#     it's not already rooted.
def root_file_reference(file_reference):
        if file_reference[0] == '/':
                return file_reference
                return getcwd() + "/" + file_reference

def usage():
        print "Usage:  compare_archive TARFILE DIRECTORY"

if len(argv) != 3:

source_archive = root_file_reference(argv[1])
root_of_altered_sources = root_file_reference(argv[2])

tempdir = "/tmp/comparison." + str(getpid())

report = ""
count = 0
system("tar xf " + source_archive)
for file in popen("find . -type f -print").readlines():
                # Chop off the terminal newline.
        file = file[:-1]
        difference = popen("diff %s %s/./%s" %
                        (file, root_of_altered_sources, file)).read()
        if difference != "":
                report = report + "-------------------------------\n"
                report = report + "Modifications in " + file
                report = report + ":\n" + difference
                count = count + 1

system("rm -rf " + tempdir)

print "A total of %s sources have been changed." % count
print report

If you want to try for yourself, simply install Python on a Unix host, save the source into a file named, set its execution bit, and launch it from the command line. You'll see a result that looks something like:

A total of 2 sources have been changed.
Modifications in ./src/first:
> This is a new line.
Modifications in ./lib/ar/mine.c:
> #include 
> #include 
> #include 

A longer production version of this utility handles more exceptions and is portable to MS-DOS.

Python vs. other scripting languages
Python is the most conventional-looking of the popular scripting languages. It scripts and reads more like a system language than a scripting language, so those who know another programming language will feel right at home. If you or your customers don't have a programming background, however, Tcl's "command language" approach might be more comfortable.

Scripting languages are slower than compiled languages, of course. Explicit compile-time declarations permit C and FORTRAN compilers to generate applications that execute faster. Python doesn't insist on explicit typing. It enlists the interpreter to figure out how numbers and other datatypes are used. Python runs slower than compiled programs, line for line. Of course, as with all scripting languages, Python programs require far fewer lines. This helps optimize the most important performance metric: the time-to-completion of a correct application. For most jobs, development time is far greater than execution time, and that's precisely where Python wins. In any case, don't let the fear of winding up with a slow-running application paralyze you. Python is comparable to or faster than all the scripting languages we've tried, and performance hasn't caused us to regret a decision to use Python yet.

As a scripting language, Python is excellent for CGI server-side Web work, small database reporting tasks, system administration, and automation of legacy components.

Here is a Python program,, which makes a convenient desktop calculator:

from Tkinter import *
from math import *

def evaluate(event):
        label['text'] = "Result:  " + str(eval(expression.get()))

frame = Frame(None)

entry = Entry(frame)
entry['textvariable'] = expression = StringVar()
entry.bind("", evaluate)

label = Label(frame)

button = Button(frame, text = "Submit", command = evaluate)


It's a tiny program, but still, is handy when working on chemical engineering or biomedicine as it allows you to define special-purpose functions on the fly and immediately apply them.

In contrast to other scripting languages, Python implements object-oriented designs naturally. While Perl, for instance, is specialized to the common tasks of system administration and is ideal for writing Unix-style filters, its object constructs are a bit "bumpier" than Python's. Python was designed to scale well, and it does.

Python's graphical user interface (GUI) bindings are interesting. Tcl's GUI extension, called Tk, is so popular that other languages, including Scheme, Perl, and Python, have adopted it as their GUI toolkit of choice. Tkinter, Python's adaptation of Tk, is popular and well-supported.

GUI programming showcases Python's object orientation, for subclassing Tk widgets is easier in Python than in Tcl itself. This same strong object orientation has encouraged an explosion of experimentation with alternative GUI toolkits. Bindings to Qt, AWT, Microsoft Foundation Classes, and a half-dozen other toolkits (or their work-alikes) are available for Python. Fans of these alternatives frequently petition van Rossum for replacement of his support of Tkinter. These pleas, and the yearly decision to keep Tkinter, are one of the minor traditions of the Python world. Tkinter appears to have a long life ahead of it, and Python will continue to be an excellent choice for those experimenting with new graphics libraries and concepts.

The main event: Python vs. C/C++ and Java
When van Rossum designed Python he avoided the mistakes he saw in traditional languages. Particularly, he cleansed Python of the problems for which C is notorious. Python's array constructs, for example, are prone to far fewer errors. Python performs automatic memory management, and therefore immediately eliminates most of the memory leaks to which C and C++ projects are prone.Python checks array references for boundary violations. "Python never dumps core because of user coding errors--you are guaranteed to get a Python traceback," as van Rossum proudly points out. Moreover, Python has powerful built-in datatypes (including tuples, dictionaries, and lists) that dramatically simplify much of conventional coding. Finally, Python's syntax is particularly simple and comprehensible. It is a very uniform language.

Here's an example of a simple program that scans a file for distinct words (sequences separated by spaces, tabs, or newlines), counts the number of times each word appears, alphabetizes the results, and prints out a nicely formatted table:


from sys import *
from string import *

        # Create an empty dictionary.
count = {}

for line in open(argv[1], 'r').readlines():
        for word in split(line):
                if count.has_key(word):
                        count[word] = count[word] + 1
                        count[word] = 1

words = count.keys()
for word in words:
        print "%15s\t%10d" % (word, count[word])

A coding in C of the same result takes a page just to declare its data structures. While this sort of textual slicing-and-dicing is a traditional strength of scripting languages (Perl can do the same in half as many lines), Python is unique in the level of readability and expressive power it retains on large, complex problems.

We have a preference for Python over C/C++ and Java because it's easier to learn, use, port, and test. More portable than Java? Absolutely! Despite Java's hype, all the major scripting languages remain more portable. This is certainly true for Python. Python is also more mature than Java. Van Rossum's conservative, consistent leadership has made Python a predictable environment in its eighth year. Java, in contrast, continues to change rapidly and even disruptively. It isn't always clear where it's going or who is steering it there. It is a chore to keep track of where it is today--a problem since versions matter significantly. Think, for example, about how much the Java ODBC application programming interface (JDBC API) changed in its first year.

Python glues. Java doesn't, at least not easily. In fact, when we need to link Java with legacy applications, we often use Python or Tcl to glue everything together. Even 100% pure Java projects are easiest done by testing Java components in development with JPython, the new implementation of Python in Java.. Scripting interactively is a far more convenient way to test than by way of fragile, special-purpose test harnesses.

What about C? C was designed to be easy for the computer. Its concepts are closer to machine language, making it easier to write a compiler. The technical joke about C is that it's a high-level assembler. With Python, time saving begins at the design stage of the project. Changes are easily conceived and implemented because Python is a systems language with gluing capabilities.

Just as with Java, we use Python even on jobs where we deliver components coded in C. It's easier to assemble and test C pieces with high-level scripting than in a conventional C environment. The results we get are more rapid and reliable.

Where's the beef?
If Python is so good, why isn't it used more? First, it's used in more places than you might expect. InfoSeek, Four11, and Musi-Cal rely on Python scripts to manage and serve up their data. The Johnson Space Center has standardized on Python for scripting its Integrated Planning System, with prospects for a greater role in the future. The American Association of University Presses implemented its online catalog in Python. The Swedish Meteorological and Hydrological Institute uses Python to manage many of its processes, including production of satellite data for daily television broadcasts.

The real question is: why isn't corporate America rushing to embrace it? True, Python's use is growing. Bank United's online loan calculator is a Python application. Still, its potential for saving time and improving quality warrant much greater awareness than Python has so far received. There are several reasons its adoption hasn't been more widespread:

  1. It's free. As much as corporations like to keep an eye on their bottom line, they like a glossy box and printed instruction manual even better -- it makes them feel safe. Companies whose primary business is not software are likely to view freeware as a risky proposition.

  2. It's not C. True, and thank goodness! Some companies simply fear the unknown. In other cases, an organization might applaud Python in principle, yet have too much invested in C to make a move. One aspect of this we've encountered is the perceived irreplaceability of "metrics" -- tools used to keep tabs on programmers and evaluate performance. In example, a company may have a C/C++-coded program to assess the quality and productivity of C/C++ applications. In such a place, and despite its potential savings, Python may never fit in.

  3. Organizations feel uncomfortable that Python doesn't have all the same tools to test that C and C++ do. Quality assurance is important, and "testability" blocks adoption of Python in some organizations. What they often don't appreciate, though, is that many of the defects they track are specific to those languages, and don't even exist in Python.

    For instance, memory allocation and memory reference errors are common in C/C++, and there are tools to test for their occurrence. There are no such products for Python because with Python the computer takes responsibility for managing memory, not the programmer. People don't program these things in Python and, consequently, people don't get them wrong. End of story. Other common errors in C/C++ have to do with explicit typing of variables. It's part of good engineering, and people talk about it a lot -- in C/C++. All typing is implicit in Python, as well as other scripting languages, so the issue simply doesn't exist.

  4. Resources may be available only to specific languages. Sophisticated use of Java security, JavaBeans, or Java threads, for example, are perhaps still best done from a Java application.

With regard to the last item, it bears reiteration that there's often a broad scope for "gluing" Java pieces with Python. With the latest release of JPython, even applet visual elements are scriptable. For example, this complete small applet, simplified from an example by Jim Hugunin, puts a couple of buttons on the screen and defines actions for them:

from java import awt, applet

class ButtonDemo(applet.Applet):
    def init(self):
        self.b1 = awt.Button('Disable middle button', actionPerformed=self.disable)
        self.b2 = awt.Button('Middle button')
        self.b3 = awt.Button('Enable middle button', enabled=0, actionPerformed=self.enable)


    def enable(self, event):
        self.b1.enabled = self.b2.enabled = 1
        self.b3.enabled = 0

    def disable(self, event):
        self.b1.enabled = self.b2.enabled = 0
        self.b3.enabled = 1

How does Python contribute to C and C++ projects? Python leverages conventional tools by gluing them together flexibly. Python simplifies writing test drivers for C++. It promotes a manageable "building block" architecture. It delivers results swiftly.

Python news
Python has been in the news lately:

Even those who've encountered Python casually in the past might want to give it a second look. The version 1.5 release on December 31, 1997 filled in a number of small gaps in Python's portfolio. As van Rossum summarized for this article, "The 1.5 release makes improvements in packaging, performance, and namespace management, all three of which are important in the competitive race." In particular:

A healthy community
There appears to be plenty of growth left in Python's future. A seventh International Python Conference is tentatively scheduled in Houston this November, and van Rossum also told us of "plans for a Python Consortium." As it grows, the links between Python and other languages will strengthen: "I expect some of the issues may be C++ integration, how best to use packages," he says.

Even with all this attention and ferment, life in Python Land retains its small-town feel. Conversations in the Usenet newsgroup comp.lang.python are consistently civil and pertinent, in contrast to what followers of the more boisterous language communities might have come to expect. Despite his packed schedule, van Rossum responds to all his e-mail.

Guido van Rossum

Python has more of an academic flavor than Perl or Tcl, which were created originally to solve specific problems, and which carry remnants of those previous lives to this day. They are clearly "evolved products." Python has also changed through the years, for van Rossum is sensitive to his users. The change has been more stately, though. Python is like Modula or Clean, in that its design was aimed first at modeling abstract virtues.

These strong theoretical underpinnings make Python particularly attractive to experimenters and researchers. The Python community takes on projects in artificial intelligence or novel computer-human interface more often than do users of the other common scripting languages. Of course, these theoretical strengths also support the power and reliability Python gives working programmers out to solve today's problems quickly.

Give Python an hour of your time and see for yourself how far you can go with it.


About the author
Cameron Laird and Kathryn Soraiz manage their own software consultancy, Network Engineered Solutions, from just outside Houston, Texas. They invite you to e-mail them for notice of upcoming articles. Reach Kathryn at Reach Cameron at

What did you think of this article?
-Very worth reading
-Worth reading
-Not worth reading
-Too long
-Just right
-Too short
-Too technical
-Just right
-Not technical enough

[Table of Contents]
Sun's Site
[Next story]
Sun's Site

[(c) Copyright  Web Publishing Inc., and IDG Communication company]

If you have technical problems with this magazine, contact

Last modified: