Originally Published: Wednesday, 30 August 2000 Author: Jason Tackaberry
Published to: develop_articles_tutorials/Development Tutorials Page: 1/6 - [Printable]

Programming with Python - Part 1: Baby Steps

This tutorial is the first in a series that will introduce you to Python, a dynamic, object-oriented language that is rapidly gaining popularity. Since I myself have garnered a modest Perl background before learning Python, I will target this tutorial at the Perl programmer. However, anyone with a little programming experience should find this text useful.

Introduction   Page 1 of 6  >>

Programming with Python series
1. Baby Steps
2. The Real World
3. Extending Python

This tutorial is the first in a series that will introduce you to Python, a dynamic, object-oriented language that is rapidly gaining popularity. Since I myself have garnered a modest Perl background before learning Python, I will target this tutorial at the Perl programmer. However, anyone with a little programming experience should find this text useful.

In the interest of full disclosure, Python is my language of choice. I have a reasonably solid background in Perl, C, and C++, but when I feel Python will do the job, I tend to favor it. So, despite my care to the contrary, some of the comments in this series may be subjective. If you have any strong objections, please mail me. We might be able to spark some interesting discussions.

In the Beginning...

Some time in the 80s, Guido van Rossum, Python's lead developer, co-authored a programming language called ABC, geared toward teaching programming concepts. ABC, while never really becoming very popular, was overhauled in its entirety, and became the proud father of Python. One of Python's core principles was and still is simplicity and elegance. More than 10 years later, Python has retained its elegance, and has only grown in features and popularity.

...and, yes, Python was indeed named after Monty Python.

So what is Python, anyway? Python is every one of these things: interpreted, dynamic (loosely typed), object-oriented, portable, clean, elegant, easy to learn, powerful, embeddable, extendable, freely available, actively developed, and widely used. Because of its simplicity and clean syntax, Python makes an excellent first language. And because of its incredibly diverse library of modules, it is an excellent language for experienced developers. Python is suitable for small projects, and scales beautifully for very large projects.

Python is commonly used to create software prototypes: versions that are used to model and prove a design, and then ported to lower-level languages or discarded. You may be surprised that one would simply discard a prototype; throw-away prototyping is a common practice in software engineering, and Python's simplicity makes this practice feasible. This case study documents Python's role in a commercial environment, used to prototype and ultimately build a modest sized system.

Getting Down to Business

Okay, enough chitchat; let's get our hands dirty. Like Perl, Python is interpreted, and so runs using an interpreter. Also, like Perl, Python code is dynamically converted to byte-code before execution. This byte-code is saved to disk so that subsequent executions need not recompile the code, unless modifications to the source code have been made, of course.

So, our first Python program can be directly typed into the interpreter, run through the interpreter by passing the source file as an argument, or by prefixing the source file with a #! directive pointing to the Python interpreter:

  #!/usr/bin/env python

Once this file is made executable, it can be run directly. The Python version of Hello World is rather uninteresting, but in the spirit of tradition, here it is, in all its glory:

  print "Hello World!"

A programming language is pretty useless without any form of flow control or iteration. However, before I introduce these constructs, I should gently ease you into one of Python's most controversial features. Whereas in C and Perl, groups of statements (blocks) are sandwiched in braces, in Python blocks are denoted solely by their indentation. Consider the following code in Perl:

  if ($foo == 1) {
    $foo = 4;
    print "Changed foo to 4";
    do_something_else();
  }

The same code would look like this in Python:

  if foo == 1:
    foo = 4
    print "Changed foo to 4"
    do_something_else()

In the Perl snippet, the whitespace preceding the lines in the code block is arbitrary. I could use any number of spaces or tabs; the choice is purely stylistic. In Python, the prefixed whitespace is mandatory. A certain degree of style is permitted: you can use any number of whitespaces or tabs. The only requirement is that you be consistent in your use. So, if the first line in the block is prefixed with a tab, you had better make sure the next line is as well, or else the interpreter will generate an error.

If you're accustomed to Perl or C, at this point you must be crying, "that's just absurd!" I know I did. But once you start using Python you quickly become at ease with this. And once you start weeding through thousands of lines of code, you begin to appreciate it. It may seem absurd now, but unless you have years of experience sorting through nests of braces, it makes the code much more readable. 3 or 4 nested blocks in Perl will look to the beginner like Lisp looks to us mere mortals.





Introduction   Page 1 of 6  >>