BUY THIS BOOK

Safari Books Online

What is this?

Looking to Reprint this content?


Learning Python
Learning Python By Mark Lutz, David Ascher
April 1999
Pages: 384

Cover | Table of Contents | Colophon


Table of Contents

Chapter 1: Getting Started
This chapter starts with a nontechnical introduction to Python and then takes a quick look at ways to run Python programs. Its main goal is to get you set up to run Python code on your own machine, so you can work along with the examples and exercises in the later chapters. Along the way, we'll study the bare essentials of Python configuration—just enough to get started. You don't have to work along with the book on your own, but we strongly encourage it if possible. Even if you can't, this chapter will be useful when you do start coding on your own.
We'll also take a quick first look at Python module files here. Most of the examples you see early in the book are typed at Python's interactive interpreter command-line. Code entered this way goes away as soon as you leave Python. If you want to save your code in a file, you need to know a bit about Python modules, so module fundamentals are introduced here. We'll save most module details for a later chapter.
If you've bought this book, chances are you already know what Python is, and why it's an important tool to learn. If not, you probably won't be sold on Python until you've learned the language by reading the rest of this book. But before jumping into details, we'd like to use a few pages to briefly introduce some of the main reasons behind Python's popularity. (Even if you don't care for nontechnical overviews, your manager might.)
Python is perhaps best described as an object-oriented scripting language: its design mixes software engineering features of traditional languages with the usability of scripting languages. But some of Python's best assets tell a more complete story.

Section 1.1.1.1: It's object-oriented

Python is an object-oriented language, from the ground up. Its class model supports advanced notions such as polymorphism, operator overloading, and multiple inheritance; yet in the context of Python's dynamic typing, object-oriented programming (OOP) is remarkably easy to apply. In fact, if you don't understand these terms, you'll find they are much easier to learn with Python than with just about any other OOP language available.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Why Python?
If you've bought this book, chances are you already know what Python is, and why it's an important tool to learn. If not, you probably won't be sold on Python until you've learned the language by reading the rest of this book. But before jumping into details, we'd like to use a few pages to briefly introduce some of the main reasons behind Python's popularity. (Even if you don't care for nontechnical overviews, your manager might.)
Python is perhaps best described as an object-oriented scripting language: its design mixes software engineering features of traditional languages with the usability of scripting languages. But some of Python's best assets tell a more complete story.

Section 1.1.1.1: It's object-oriented

Python is an object-oriented language, from the ground up. Its class model supports advanced notions such as polymorphism, operator overloading, and multiple inheritance; yet in the context of Python's dynamic typing, object-oriented programming (OOP) is remarkably easy to apply. In fact, if you don't understand these terms, you'll find they are much easier to learn with Python than with just about any other OOP language available.
Besides serving as a powerful code structuring and reuse device, Python's OOP nature makes it ideal as a scripting tool for object-oriented systems languages such as C++ and Java. For example, with the appropriate glue code, Python programs can subclass (specialize) classes implemented in C++ or Java. Of equal significance, OOP is an option in Python; you can go far without having to become an object guru all at once.

Section 1.1.1.2: It's free

Python is freeware—something which has lately been come to be called open source software. As with Tcl and Perl, you can get the entire system for free over the Internet. There are no restrictions on copying it, embedding it in your systems, or shipping it with your products. In fact, you can even sell Python, if you're so inclined.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
How to Run Python Programs
So far, we've mostly talked about Python as a programming language. But it's also a software package called an interpreter . An interpreter is a kind of program that executes other programs. When you write Python programs, the Python interpreter reads your program, and carries out the instructions it contains. In this section we explore ways to tell the Python interpreter which programs to run.
When the Python package is installed on your machine, it generates a number of components. Depending on how you use it, the Python interpreter may take the form of an executable program, or a set of libraries linked into another program. In general, there are at least five ways to run programs through the Python interpreter:
  • Interactively
  • As Python module files
  • As Unix-style script files
  • Embedded in another system
  • Platform-specific launching methods
Let's look at each of these strategies in turn.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
A First Look at Module Files
Earlier in this chapter, we saw how to run module files (i.e., text files containing Python statements) from the operating-system shell's command line. It turns out that we can also run module files from Python's interactive command line by importing or reloading them, as we'd normally do from other system components. The details of this process are covered in Chapter 5, but since this turns out to be a convenient way to save and run examples, we'll give a quick introduction to the process.
The basic idea behind importing modules is that importers may gain access to names assigned at the top level of a module file. The names are usually assigned to services exported by the modules. For instance, suppose we use our favorite text editor to create the one-line Python module file myfile.py, shown in the following code snippet. This may be one of the world's simplest Python modules, but it's enough to illustrate basic module use:
title = "The Meaning of Life"
Notice that the filename has a .py suffix: this naming convention is required for files imported from other components. Now we can access this module's variable title in other components two different ways, either by importing the module as a whole with an import statement and qualifying the module by the variable name we want to access:
% python                        
               Start Python
            
>>> import myfile               
               Run file, load module as a whole
            
>>> print myfile.title          
               Use its names: '.' qualification
            
The Meaning of Life
or by fetching (really, copying) names out of a module with from statements:
% python                        
               Start Python
            
               >>> 
               from myfile import title    
               Run file, load its names
            
>>> print title                 
               Use name directly: no need to qualify
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Python Configuration Details
So far, we've seen how to make the Python interpreter execute programs we've typed. But besides the interpreter, a Python installation also comes with a collection of utility programs, stored in the Python source library. Moreover, the Python interpreter recognizes settings in the system shell's environment, which let us tailor the interpreter's behavior (where it finds the source-code files, for example). This section talks about the environment settings commonly used by Python programmers, peeks at Python installation details, and presents an example script that illustrates most of the configuration steps you'll probably need to know about. If you have access to a ready-to-run Python, you can probably skip much of this section, or postpone it for a later time.
The Python interpreter recognizes a handful of environment variable settings, but only a few are used often enough to warrant explanation here. Table 1.1 summarizes the main Python variable settings.
Table 1.1: Important Environment Variables
Role
Variable
System shell search path (for finding "python")
PATH (or path)
Python module search path (for imports)
PYTHONPATH
Path to Python interactive startup file
PYTHONSTARTUP
GUI extension variables (Tkinter)
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Summary
In this chapter, we've explored ways to launch Python programs, the basics of Python module files and namespace inspection, and Python configuration and installation details. Hopefully, you should now have enough information to start interacting with the Python interpreter. In Chapter 2, we explore basic object types in Python, before looking at statements and larger program components.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Exercises
Okay: time to start doing a little coding on your own. This session is fairly simple, but a few of these questions hint at topics to come in later chapters. Remember, check Appendix C, for the answers; they sometimes contain supplemental information not discussed in the chapters. In other words, you should peek, even if you can manage to get all the answers on your own.
  1. Interaction. Start the Python command line, and type the expression: "Hello World!" (including the quotes). The string should be echoed back to you. The purpose of this exercise is to get your environment configured to run Python. You may need to add the path to the python executable to your PATH environment variable. Set it in your .cshrc or .kshrc file to make Python permanently available on Unix systems; use a setup.bat or autoexec.bat file on Windows.
  2. Programs. With the text editor of your choice, write a simple module file—a file containing the single statement: print 'Hello module world!'. Store this statement in a file named module1.py. Now, run this file by passing it to the Python interpreter program on the system shell's command line.
  3. Modules. Next, start the Python command line and import the module you wrote in the prior exercise. Does your PYTHONPATH setting include the directory where the file is stored? Try moving the file to a different directory and importing it again; what happens? (Hint: is there still a file named module1.pyc in the original directory?)
  4. Scripts. If your platform supports it, add the #! line to the top of your module1.py module, give the file executable privileges, and run it directly as an executable. What does the first line need to contain?
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 2: Types and Operators
This chapter begins our tour of the Python language. From an abstract perspective, in Python we write programs that do things with stuff. Programs take the form of statements, which we'll meet later. Here, we're interested in the stuff our programs do things to. And in Python, stuff always takes the form of objects. They may be built-in kinds of objects Python provides for us, or objects we create using Python or C tools. Either way, we're always doing things to objects in Python.
Naturally, there's more to Python development than doing things to stuff. But since the subjects of Python programs are the most fundamental notion in Python programming, we start with a survey of Python's built-in object types.
By way of introduction, let's first get a clear picture of how what we study in this chapter fits into the overall Python picture. From a more concrete perspective, Python programs can be decomposed into modules, statements, and objects, as follows:
  1. Programs are composed of modules.
  2. Modules contain statements.
  3. Statements create and process objects.
If you've used lower-level languages such as C or C++, you know that much of your work centers on implementing objects—what some folks call data structures—to represent the components in your application's domain. You need to lay out memory structures, manage memory allocation, implement search and access routines, and so on. These chores are about as tedious (and error prone) as they sound, and usually distract from your programs' real goals.
In typical Python programs, most of this grunt work goes away. Because Python provides powerful object types as an intrinsic part of the language, there's no need to code object implementations before you start solving problems. In fact, unless you have a need for special processing that built-in types don't provide, you're almost always better off using a built-in object instead of implementing your own. Here are some reasons why:
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Python Program Structure
By way of introduction, let's first get a clear picture of how what we study in this chapter fits into the overall Python picture. From a more concrete perspective, Python programs can be decomposed into modules, statements, and objects, as follows:
  1. Programs are composed of modules.
  2. Modules contain statements.
  3. Statements create and process objects.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Why Use Built-in Types?
If you've used lower-level languages such as C or C++, you know that much of your work centers on implementing objects—what some folks call data structures—to represent the components in your application's domain. You need to lay out memory structures, manage memory allocation, implement search and access routines, and so on. These chores are about as tedious (and error prone) as they sound, and usually distract from your programs' real goals.
In typical Python programs, most of this grunt work goes away. Because Python provides powerful object types as an intrinsic part of the language, there's no need to code object implementations before you start solving problems. In fact, unless you have a need for special processing that built-in types don't provide, you're almost always better off using a built-in object instead of implementing your own. Here are some reasons why:
Built-in objects make simple programs easy to write
For simple tasks, built-in types are often all you need to represent the structure of problem domains. Because we get things such as collections (lists) and search tables (dictionaries) for free, you can use them immediately. You can get a lot of work done with just Python's built-in object types alone.
Python provides objects and supports extensions
In some ways, Python borrows both from languages that rely on built-in tools (e.g., LISP), and languages that rely on the programmer to provide tool implementations or frameworks of their own (e.g., C++). Although you can implement unique object types in Python, you don't need to do so just to get started. Moreover, because Python's built-ins are standard, they're always the same; frameworks tend to differ from site to site.
Built-in objects are components of extensions
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Numbers
On to the nitty-gritty. The first object type on our tour is Python numbers. In general, Python's number types are fairly typical and will seem familiar if you've used just about any other programming language in the past. Python supports the usual numeric types (integer and floating point), constants, and expressions. In addition, Python provides more advanced numeric programming support, including a complex number type, an unlimited precision integer, and a variety of numeric tool libraries. The next few sections give an overview of the numeric support in Python.
Among its basic types, Python supports the usual suspects: both integer and floating-point numbers, and all their associated syntax and operations. Like C, Python also allows you to write integers using hexadecimal and octal constants. Unlike C, Python also has a complex number type (introduced in Python 1.4), as well as a long integer type with unlimited precision (it can grow to have as many digits as your memory space allows). Table 2.2 shows what Python's numeric types look like when written out in a program (i.e., as constants).
Table 2.2: Numeric Constants
Constant
Interpretation
1234, -24, 0
Normal integers (C longs)
999999999999L
Long integers (unlimited size)
1.23, 3.14e-10, 4E210, 4.0e+210
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Strings
The next major built-in type is the Python string —an ordered collection of characters, used to store and represent text-based information. From a functional perspective, strings can be used to represent just about anything that can be encoded as text: symbols and words (e.g., your name), contents of text files loaded into memory, and so on.
You've probably used strings in other languages too; Python's strings serve the same role as character arrays in languages such as C, but Python's strings are a higher level tool. Unlike C, there is no char type in Python, only one-character strings. And strictly speaking, Python strings are categorized as immutable sequences— big words that just mean that they respond to common sequence operations but can't be changed in place. In fact, strings are representative of the larger class of objects called sequences; we'll have more to say about what this means in a moment, but pay attention to the operations introduced here, because they'll work the same on types we'll see later.
Table 2.4 introduces common string constants and operations. Strings support expression operations such as concatenation (combining strings), slicing (extracting sections), indexing (fetching by offset), and so on. Python also provides a set of utility modules for processing strings you import. For instance, the string module exports most of the standard C library's string handling tools, and the regex and re modules add regular expression matching for strings (all of which are discussed in Chapter 8).
Table 2.4: Common String Constants and Operations
Operation
Interpretation
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Lists
Our next stop on the built-in object tour is the Python list. Lists are Python's most flexible ordered collection object type. Unlike strings, lists can contain any sort of object: numbers, strings, even other lists. Python lists do the work of most of the collection data structures you might have to implement manually in lower-level languages such as C. In terms of some of their main properties, Python lists are:
Ordered collections of arbitrary objects
From a functional view, lists are just a place to collect other objects, so you can treat them as a group. Lists also define a left-to-right positional ordering of the items in the list.
Accessed by offset
Just as with strings, you can fetch a component object out of a list by indexing the list on the object's offset. Since lists are ordered, you can also do such tasks as slicing and concatenation.
Variable length, heterogeneous, arbitrarily nestable
Unlike strings, lists can grow and shrink in place (they're variable length), and may contain any sort of object, not just one-character strings (they're heterogeneous). Because lists can contain other complex objects, lists also support arbitrary nesting; you can create lists of lists of lists, and so on.
Of the category mutable sequence
In terms of our type category qualifiers, lists can be both changed in place (they're mutable) and respond to all the sequence operations we saw in action on strings in the last section. In fact, sequence operations work the same on lists, so we won't have much to say about them here. On the other hand, because lists are mutable, they also support other operations strings don't, such as deletion, index assignment, and methods.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Dictionaries
Besides lists, dictionaries are perhaps the most flexible built-in data type in Python. If you think of lists as ordered collections of objects, dictionaries are unordered collections; their chief distinction is that items are stored and fetched in dictionaries by key, instead of offset. As we'll see, built-in dictionaries can replace many of the searching algorithms and data-structures you might have to implement manually in lower-level languages. Dictionaries also sometimes do the work of records and symbol tables used in other languages. In terms of their main properties, dictionaries are:
Accessed by key, not offset
Dictionaries are sometimes called associative arrays or hashes. They associate a set of values with keys, so that you can fetch an item out of a dictionary using the key that stores it. You use the same indexing operation to get components in a dictionary, but the index takes the form of a key, not a relative offset.
Unordered collections of arbitrary objects
Unlike lists, items stored in a dictionary aren't kept in any particular order; in fact, Python randomizes their order in order to provide quick lookup. Keys provide the symbolic (not physical) location of items in a dictionary.
Variable length, heterogeneous, arbitrarily nestable
Like lists, dictionaries can grow and shrink in place (without making a copy), they can contain objects of any type, and support nesting to any depth (they can contain lists, other dictionaries, and so on).
Of the category mutable mapping
They can be changed in place by assigning to indexes, but don't support the sequence operations we've seen work on strings and lists. In fact, they can't: because dictionaries are unordered collections, operations that depend on a fixed order (e.g., concatenation, slicing) don't make sense. Instead, dictionaries are the only built-in representative of the mapping type category—objects that map keys to values.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Tuples
The last collection type in our survey is the Python tuple. Tuples construct simple groups of objects. They work exactly like lists, except that tuples can't be changed in place (they're immutable) and are usually written as a series of items in parentheses, not square brackets. Tuples share most of their properties with lists. They are:
Ordered collections of arbitrary objects
Like strings and lists, tuples are an ordered collection of objects; like lists, they can embed any kind of object.
Accessed by offset
Like strings and lists, items in a tuple are accessed by offset (not key); they support all the offset-base access operations we've already seen, such as indexing and slicing.
Of the category immutable sequence
Like strings, tuples are immutable; they don't support any of the in-place change operations we saw applied to lists. Like strings and lists, tuples are sequences; they support many of the same operations.
Fixed length, heterogeneous, arbitrarily nestable
Because tuples are immutable, they can't grow or shrink without making a new tuple; on the other hand, tuples can hold other compound objects (e.g., lists, dictionaries, other tuples) and so support nesting.
Arrays of object references
Like lists, tuples are best thought of as object reference arrays; tuples store access points to other objects (references), and indexing a tuple is relatively quick.
Table 2.9 highlights common tuple operations. Tuples are written as a series of objects (really, expressions), separated by commas, and enclosed in parentheses. An empty tuple is just a parentheses pair with nothing inside.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Files
Hopefully, most readers are familiar with the notion of files—named storage compartments on your computer that are managed by your operating system. Our last built-in object type provides a way to access those files inside Python programs. The built-in open function creates a Python file object, which serves as a link to a file residing on your machine. After calling open, you can read and write the associated external file, by calling file object methods.
Compared to types we've seen so far, file objects are somewhat unusual. They're not numbers, sequences, or mappings; instead, they export methods only for common file processing tasks. Technically, files are a prebuilt C extension type that provides a thin wrapper over the underlying C stdio filesystem; in fact, file object methods have an almost 1-to-1 correspondence to file functions in the standard C library.
Table 2.10 summarizes common file operations. To open a file, a program calls the open function, with the external name first, followed by a processing mode ('r' means open for input, 'w' means create and open for output, 'a' means open for appending to the end, and others we'll ignore here). Both arguments must be Python strings.
Table 2.10: Common File Operations
Operation
Interpretation
output = open('/tmp/spam', 'w')
Create output file ('w' means write)
input = open('data', 'r')
Create input file ('r' means read)
S = input.read()
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
General Object Properties
Now that we've seen all of Python's built-in types, let's take a quick look at some of the properties they share. Some of this section is a review of ideas we've already seen at work.
Table 2.11 classifies all the types we've seen, according to the type categories we introduced earlier. As we've seen, objects share operations according to their category—for instance, strings, lists, and tuples all share sequence operations. As we've also seen, only mutable objects may be changed in place. You can change lists and dictionaries in place, but not numbers, strings, or tuples. Files only export methods, so mutability doesn't really apply (they may be changed when written, but this isn't the same as Python type constraints).
Table 2.11: Object Classifications
Object type
Category
Mutable?
Numbers
Numeric
No
Strings
Sequence
No
Lists
Sequence
Yes
Dictionaries
Mapping
Yes
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Built-in Type Gotchas
In this and most of the next few chapters, we'll include a discussion of common problems that seem to bite new users (and the occasional expert), along with their solutions. We call these gotchas—a degenerate form of "got you"—because some may catch you by surprise, especially when you're just getting started with Python. Others represent esoteric Python behavior, which comes up rarely (if ever!) in real programming, but tends to get an inordinate amount of attention from language aficionados on the Internet (like us). Either way, all have something to teach us about Python; if you can understand the exceptions, the rest is easy.
We've talked about this earlier, but we want to mention it again here, to underscore that it can be a gotcha if you don't understand what's going on with shared references in your program. For instance, in the following, the list object assigned to name L is referenced both from L and from inside the list assigned to name M. Changing L in place changes what M references too:
>>> L = [1, 2, 3]
>>> M = ['X', L, 'Y']       # embed a reference to L
>>> M
['X', [1, 2, 3], 'Y']

>>> L[1] = 0                # changes M too
>>> M
['X', [1, 0, 3], 'Y']

Section 2.10.1.1: Solutions

This effect usually becomes important only in larger programs, and sometimes shared references are exactly what you want. If they're not, you can avoid sharing objects by copying them explicitly; for lists, you can always make a top-level copy by using an empty-limits slice:
>>> L = [1, 2, 3]
>>> M = ['X', L[:], 'Y']       # embed a copy of L
>>> L[1] = 0                   # only changes L, not M 
>>> L
[1, 0, 3]
>>> M
['X', [1, 2, 3], 'Y']
Remember, slice limits default to and the length of the sequence being sliced; if both are omitted, the slice extracts every item in the sequence, and so makes a top-level copy (a new, unshared object).
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Summary
In this chapter, we've met Python's built-in object types—numbers, strings, lists, dictionaries, tuples, and files—along with the operations Python provides for processing them. We've also noticed some of the themes underlying objects in Python along the way; in particular, the notions of operation overloading and type categories help to simplify types in Python. Finally, we've seen a few common pitfalls of built-in types.
Almost all the examples in this chapter were deliberately artificial to illustrate the basics. In the next chapter, we'll start studying statements that create and process objects and let us build up programs that do more realistic work.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Exercises
This session asks you to get your feet wet with built-in object fundamentals. As before, a few new ideas may pop up along the way, so be sure to flip to Appendix C when you're done (and even when you're not).
  1. The basics. Experiment interactively with the common type operations found in this chapter's tables. To get you started, bring up the Python interactive interpreter, type the expressions below, and try to explain what's happening in each case:
    2 ** 16
    2 / 5, 2 / 5.0
    
    "spam" + "eggs"
    S = "ham"
    "eggs " + S
    S * 5
    S[:0]
    "green %s and %s" % ("eggs", S)
    
    ('x',)[0]
    ('x', 'y')[1]
    
    L = [1,2,3] + [4,5,6]
    L, L[:], L[:0], L[-2], L[-2:]
    ([1,2,3] + [4,5,6])[2:4]
    [L[2], L[3]]
    L.reverse(); L
    L.sort(); L
    L.index(4)
    
    {'a':1, 'b':2}['b']
    D = {'x':1, 'y':2, 'z':3}
    D['w'] = 0
    D['x'] + D['w']
    D[(1,2,3)] = 4
    D.keys(), D.values(), D.has_key((1,2,3))
    
    [[]], ["",[],(),{},None]
  2. Indexing and slicing. At the interactive prompt, define a list named L that contains four strings or numbers (e.g., L=[0,1,2,3]). Now, let's experiment with some boundary cases.
    1. What happens when you try to index out of bounds (e.g., L[4])?
    2. What about slicing out of bounds (e.g., L[-1000:100])?
    3. Finally, how does Python handle it if you try to extract a sequence in reverse—with the lower bound greater than the higher bound (e.g., L[3:1])? Hint: try assigning to this slice (L[3:1] = ['?']) and see where the value is put. Do you think this may be the same phenomenon you saw when slicing out of bounds?
  3. Indexing, slicing, and del. Define another list L with four items again, and assign an empty list to one of its offsets (e.g.,
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 3: Basic Statements
Now that we've seen Python's fundamental built-in object types, we're going to move on in this chapter to explore its basic statement types. In simple terms, statements are the things you write to tell Python what your programs should do. If programs do things with stuff, statements are the way you specify what sort of things a program does. By and large, Python is a procedural, statement-based language; by combining statements, you specify a procedure Python performs to satisfy a program's goals.
Another way to understand the role of statements is to revisit the concept hierarchy we introduced in Chapter 2. In that chapter we talked about built-in objects; now we climb the hierarchy to the next level:
  1. Programs are composed of modules.
  2. Modules contain statements.
  3. Statements create and process objects.
Statements process the objects we've already seen. Moreover, statements are where objects spring into existence (e.g., in assignment statement expressions), and some statements create entirely new kinds of objects (functions, classes, and so on). And although we won't discuss this in detail until Chapter 5, statements always exist in modules, which themselves are managed with statements.
Table 3.1 summarizes Python's statement set. We've introduced a few of these already; for instance, in Chapter 2, we saw that the del statement deletes data structure components, the assignment statement creates references to objects, and so on. In this chapter, we fill in details that were skipped and introduce the rest of Python's basic procedural statements. We stop short when statements that have to do with larger program units—functions, classes, modules, and exceptions—are reached. Since these statements lead to more sophisticated programming ideas, we'll give them each a chapter of their own. More exotic statements like exec (which compiles and executes code we create as strings) and
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Assignment
We've been using the Python assignment statement already, to assign objects to names. In its basic form, you write a target of an assignment on the left of an equals sign and an object to be assigned on the right. The target on the left may be a name or object component, and the object on the right can be an arbitrary expression that computes an object. For the most part, assignment is straightforward to use, but here are a few properties to keep in mind:
Assignments create object references
As we've already seen, Python assignment stores references to objects in names or data structure slots. It always creates references to objects, instead of copying objects. Because of that, Python variables are much more like pointers than data storage areas as in C.
Names are created when first assigned
As we've also seen, Python creates variable names the first time you assign them a value (an object reference). There's no need to predeclare names ahead of time. Some (but not all) data structure slots are created when assigned too (e.g., dictionary entries, some object attributes). Once assigned, a name is replaced by the value it references when it appears in an expression.
Names must be assigned before being referenced
Conversely, it's an error to use a name you haven't assigned a value to yet. Python raises an exception if you try, rather than returning some sort of ambiguous (and hard to notice) default value.
Implicit assignments: import, from, def, class, for, function arguments, etc.
In this section, we're concerned with the = statement, but assignment occurs in many contexts in Python. For instance, we'll see later that module imports, function and class definitions,
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Expressions
In Python, you can use expressions as statements too. But since the result of the expression won't be saved, it makes sense to do so only if the expression does something useful as a side effect. Expressions are commonly used as statements in two situations:
For calls to functions and methods
Some functions and methods do lots of work without returning a value. Since you're not interested in retaining the value they return, you can call such functions with an expression statement. Such functions are sometimes called procedures in other languages; in Python, they take the form of functions that don't return a value.
For printing values at the interactive prompt
As we've already seen, Python echoes back the results of expressions typed at the interactive command line. Technically, these are expression statements too; they serve as a shorthand for typing print statements.
Table 3.3 lists some common expression statement forms in Python; we've seen most before. Calls to functions and methods are coded with a list of objects (really, expressions that evaluate to objects) in parentheses after the function or method.
Table 3.3: Common Python Expression Statements
Operation
Interpretation
spam(eggs, ham)
Function calls
spam.ham(eggs)
Method calls
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Print
The print statement simply prints objects. Technically, it writes the textual representation of objects to the standard output stream. The standard output stream happens to be the same as the C stdout stream and usually maps to the window where you started your Python program (unless you've redirected it to a file in your system's shell).
In Chapter 2, we also saw file methods that write text. The print statement is similar, but more focused: print writes objects to the stdout stream (with some default formatting), but file write methods write strings to files. Since the standard output stream is available in Python as the stdout object in the built-in sys module (aka sys.stdout), it's possible to emulate print with file writes (see below), but print is easier to use.
Table 3.4 lists the print statement's forms.
Table 3.4: Print Statement Forms
Operation
Interpretation
print spam, ham
Print objects to sys.stdout, add a space between
print spam, ham,
Same, but don't add newline at end
By default, print adds a space between items separated by commas and adds a linefeed at the end of the current output line. To suppress the linefeed (so you can add more text on the same line later), end your print statement with a comma, as shown in the second line of the table. To suppress the space between items, you can instead build up an output string using the string concatenation and formatting tools in Chapter 2:
>>> print "a", "b"
a b
>>> 
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
if Tests
The Python if statement selects actions to perform. It's the primary selection tool in Python and represents much of the logic a Python program possesses. It's also our first compound statement; like all compound Python statements, the if may contain other statements, including other ifs. In fact, Python lets you combine statements in a program both sequentially (so that they execute one after another), and arbitrarily nested (so that they execute only under certain conditions).
The Python if statement is typical of most procedural languages. It takes the form of an if test, followed by one or more optional elif tests (meaning "else if"), and ends with an optional else block. Each test and the else have an associated block of nested statements indented under a header line. When the statement runs, Python executes the block of code associated with the first test that evaluates to true, or the else block if all tests prove false. The general form of an if looks like this:
if <test1>:               # if test 
    <statements1>         # associated block
elif <test2>:             # optional elif's 
    <statements2>
else:                     # optional else
    <statements3>
Here are two simple examples of the if statement. All parts are optional except the initial if test and its associated statements. Here's the first:
>>> if 1:
...     print 'true'
...
true
>>> if not 1:
...     print 'true'
... else:
...     print 'false'
...
false
Now, here's an example of the most complex kind of if statement—with all its optional parts present. The statement extends from the if line, through the else's block. Python executes the statements nested under the first test that is true, or else the else part. In practice, both the elif and else parts may be omitted, and there may be more than one statement nested in each section:
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
while Loops
Python's while statement is its most general iteration construct. In simple terms, it repeatedly executes a block of indented statements, as long as a test at the top keeps evaluating to a true value. When the test becomes false, control continues after all the statements in the while, and the body never runs if the test is false to begin with.
The while statement is one of two looping statements (along with the for, which we'll meet next). We call it a loop, because control keeps looping back to the start of the statement, until the test becomes false. The net effect is that the loop's body is executed repeatedly while the test at the top is true. Python also provides a handful of tools that implicitly loop (iterate), such as the map, reduce, and filter functions, and the in membership test; we explore some of these later in this book.
In its most complex form, the while statement consists of a header line with a test expression, a body of one or more indented statements, and an optional else part that is executed if control exits the loop without running into a break statement (more on these last few words later). Python keeps evaluating the test at the top, and executing the statements nested in the while part, until the test returns a false value:
while <test>:             # loop test
    <statements1>         # loop body
else:                     # optional else
    <statements2>         # run if didn't exit loop with break
To illustrate, here are a handful of simple while loops in action. The first just prints a message forever, by nesting a print statement in a while loop. Recall that an integer 1 means true; since the test is always true, Python keeps executing the body forever or until you stop its execution. This sort of behavior is usually called an infinite loop (and tends to be much less welcome when you don't expect it):
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
for Loops
The for loop is a generic sequence iterator in Python: it can step through the items in any object that responds to the sequence indexing operation. The for works on strings, lists, tuples, and new objects we'll create later with classes. We've already seen the for in action, when we mentioned the iteration operation for sequence types in Chapter 2. Here, we'll fill in the details we skipped earlier.
The Python for loop begins with a header line that specifies an assignment target (or targets), along with an object you want to step through. The header is followed by a block of indented statements, which you want to repeat:
for <target> in <object>:   # assign object items to target
    <statements>            # repeated loop body: use target
else:
    <statements>            # if we didn't hit a 'break'
When Python runs a for loop, it assigns items in the sequence object to the target, one by one, and executes the loop body for each. The loop body typically uses the assignment target to refer to the current item in the sequence, as though it were a cursor stepping through the sequence. Technically, the for works by repeatedly indexing the sequence object on successively higher indexe