This book provides an introduction to the Python programming language. Python is a popular open source programming language used for both standalone programs and scripting applications in a wide variety of domains. It is free, portable, powerful, and remarkably easy and fun to use. Programmers from every corner of the software industry have found Pythonís focus on developer productivity and software quality to be a strategic advantage in projects both large and small.

Whether you are new to programming or a professional developer, this bookís goal is to bring you quickly up to speed on the fundamentals of the core Python language. After reading this book, you will know enough about Python to apply it in whatever application domains you choose to explore.

By design, this book is a tutorial that focuses on the core Python language itself, rather than specific applications of it. As such, itís intended to serve as the first in a two-volume set:

         Learning Python, this book, teaches Python itself.

         Programming Python , among others, shows what you can do with Python after youíve learned it.

That is, applications-focused books such as Programming Pythonpick up where this book leaves off, exploring Pythonís role in common domains such as the Web, graphical user interfaces (GUIs), and databases. In addition, the book Python Pocket Referenceprovides additional reference materials not included here, and is designed to supplement this book.

Because of this bookís foundations focus, though, it is able to present Python fundamentals with more depth than many programmers see when first learning the language. And because itís based upon a three-day Python training class with quizzes and exercises throughout, this book serves as a self-paced introduction to the language.

About This Fourth Edition

This fourth edition of this book has changed in three ways. This edition:

         Covers both Python 3.0 and Python 2.6óit emphasizes 3.0, but notes differences in 2.6

         Includes a set of new chapters mainly targeted at advanced core-language topics

         Reorganizes some existing material and expands it with new examples for clarity

As I write this edition in 2009, Python comes in two flavorsóVersion 3.0 is an emerging and incompatible mutation of the language, and 2.6 retains backward compatibility with the vast body of existing Python code. Although Python 3 is viewed as the future of Python, Python 2 is still widely used and will be supported in parallel with Python 3 for years to come. While 3.0 is largely the same language, it runs almost no code written for prior releases (the mutation of print from statement to function alone, aesthetically sound as it may be, breaks nearly every Python program ever written).

This split presents a bit of a dilemma for both programmers and book authors. While it would be easier for a book to pretend that Python 2 never existed and cover 3 only, this would not address the needs of the large Python user base that exists today. A vast amount of existing code was written for Python 2, and it wonít be going away any time soon. And while newcomers to the language can focus on Python 3, anyone who must use code written in the past needs to keep one foot in the Python 2 world today. Since it may be years before all third-party libraries and extensions are ported to Python 3, this fork might not be entirely temporary.

Coverage for Both 3.0 and 2.6

To address this dichotomy and to meet the needs of all potential readers, this edition of this book has been updated to cover both Python 3.0 and Python 2.6 (and later releases in the 3.X and 2.X lines). Itís intended for programmers using Python 2, programmers using Python 3, and programmers stuck somewhere between the two.

That is, you can use this book to learn either Python line. Although the focus here is on 3.0 primarily, 2.6 differences and tools are also noted along the way for programmers using older code. While the two versions are largely the same, they diverge in some important ways, and Iíll point these out along the way.

For instance, Iíll use 3.0 print calls in most examples, but will describe the 2.6 print statement too so you can make sense of earlier code. Iíll also freely introduce new features, such as the nonlocal statement in 3.0 and the string format method in 2.6 and 3.0, and will point out when such extensions are not present in older Pythons.

If you are learning Python for the first time and donít need to use any legacy code, I encourage you to begin with Python 3.0; it cleans up some longstanding warts in the language, while retaining all the original core ideas and adding some nice new tools. Many popular Python libraries and tools will likely be available for Python 3.0 by the time you read these words, especially given the file I/O performance improvements expected in the upcoming 3.1 release. If you are using a system based on Python 2.X, however, youíll find that this book addresses your concerns too and will help you migrate to 3.0 in the future.

By proxy, this edition addresses other Python Version 2 and 3 releases too, though some older version 2.X code may not be able to run all the examples here. Although class decorators are available in both Python 2.6 and 3.0, for example, you cannot use them in an older Python 2.X that did not yet have this feature. See the tables later in this Preface for summaries of 2.6 and 3.0 changes.

Shortly before going to press, this book was also augmented with notes about prominent extensions in the upcoming Python 3.1 releaseócomma separators and automatic field numbering in string format method calls; multiple context manager syntax in with statements; new methods for numbers; and so on. Because Python 3.1 was targeted primarily toward optimization, this book applies directly to it as well. In fact, because Python 3.1 supersedes 3.0, and because the latest Python is usually the best Python to fetch and use anyhow, in this book the term ďPython 3.0Ē generally refers to the language variations introduced by Python 3.0 but that are present in the entire 3.X line.

New Chapters

Although the main purpose of this edition is to update the examples and material from the preceding edition for 3.0 and 2.6, Iíve also added five new chapters to address new topics and add context:

         Chapter 27 is a new class tutorial, using a more realistic example to explore the basics of Python object-oriented programming (OOP).

         Chapter 36 provides details on Unicode and byte strings and outlines string and file differences between 3.0 and 2.6.

         Chapter 37 collects managed attribute tools such as properties and provides new coverage of descriptors.

         Chapter 38 presents function and class decorators and works through comprehensive examples.

         Chapter 39 covers metaclasses and compares and contrasts them with decorators.

The first of these chapters provides a gradual, step-by-step tutorial for using classes and OOP in Python. Itís based upon a live demonstration I have been using in recent years in the training classes I teach, but has been honed here for use in a book. The chapter is designed to show OOP in a more realistic context than earlier examples and to illustrate how class concepts come together into larger, working programs. I hope it works as well here as it has in live classes.

The last four of these new chapters are collected in a new final part of the book, ďAdvanced Topics.Ē Although these are technically core language topics, not every Python programmer needs to delve into the details of Unicode text or metaclasses. Because of this, these four chapters have been separated out onto this new part, and are officially optional reading. The details of Unicode and binary data strings, for example, have been moved to this final part because most programmers use simple ASCII strings and donít need to know about these topics. Similarly, decorators and metaclasses are specialist topics that are usually of more interest to API builders than application programmers.

If you do use such tools, though, or use code that does, these new advanced topic chapters should help you master the basics. In addition, these chaptersí examples include case studies that tie core language concepts together, and they are more substantial than those in most of the rest of the book. Because this new part is optional reading, it has end-of-chapter quizzes but no end-of-part exercises.

Changes to Existing Material

In addition, some material from the prior edition has been reorganized, or supplemented with new examples. Multiple inheritance, for instance, gets a new case study example that lists class trees in Chapter 30; new examples for generators that manually implement map and zip are provided in Chapter 20; static and class methods are illustrated by new code in Chapter 31; package relative imports are captured in action in Chapter 23; and the __contains__, __bool__, and __index__ operator-overloading methods are illustrated by example now as well in Chapter 29, along with new the overloading protocols for slicing and comparison.

This edition also incorporates some reorganization for clarity. For instance, to accommodate new material and topics, and to avoid chapter topic overload, five prior chapters have been split into two each here. The result is new standalone chapters on operator overloading, scopes and arguments, exception statement details, and comprehension and iteration topics. Some reordering has been done within the existing chapters as well, to improve topic flow.

This edition also tries to minimize forward references with some reordering, though Python 3.0ís changes make this impossible in some cases: to understand printing and the string format method, you now must know keyword arguments for functions; to understand dictionary key lists and key tests, you must now know iteration; to use exec to run code you need to be able to use file objects; and so on. A linear reading still probably makes the most sense, but some topics may require nonlinear jumps and random lookups.

All told, there have been hundreds of changes in this edition. The next sectionís tables alone document 27 additions and 56 changes in Python. In fact, itís fair to say that this edition is somewhat more advanced, because Python is somewhat more advanced. As for Python 3.0 itself, though, youíre probably better off discovering most of this bookís changes for yourself, rather than reading about them further in this Preface.

Specific Language Extensions in 2.6 and 3.0

In general, Python 3.0 is a cleaner language, but it is also in some ways a more sophisticated language. In fact, some of its changes seem to assume you must already know Python in order to learn Python! The prior section outlined some of the more prominent circular knowledge dependencies in 3.0; as a random example, the rationale for wrapping dictionary views in a list call is incredibly subtle and requires substantial foreknowledge. Besides teaching Python fundamentals, this book serves to help bridge this knowledge gap.

Table P-1 lists the most prominent new language features covered in this edition, along with the primary chapters in which they appear.

Table P-1. Extensions in Python 2.6 and 3.0


Covered in Chapter(s)

The print function in 3.0


The nonlocal x,y statement in 3.0


The str.format method in 2.6 and 3.0


String types in 3.0: str for Unicode text, bytes for binary data

7, 36

Text and binary file distinctions in 3.0

9, 36

Class decorators in 2.6 and 3.0: @private('age')

31, 38

New iterators in 3.0: range, map, zip

14, 20

Dictionary views in 3.0: D.keys, D.values, D.items

8, 14

Division operators in 3.0: remainders, / and //


Set literals in 3.0: {a, b, c}


Set comprehensions in 3.0: {x**2 for x in seq}

4, 5, 14, 20

Dictionary comprehensions in 3.0: {x: x**2 for x in seq}

4, 8, 14, 20

Binary digit-string support in 2.6 and 3.0: 0b0101, bin(I)


The fraction number type in 2.6 and 3.0: Fraction(1, 3)


Function annotations in 3.0: def f(a:99, b:str)->int


Keyword-only arguments in 3.0: def f(a, *b, c, **d)

18, 20

Extended sequence unpacking in 3.0: a, *b = seq

11, 13

Relative import syntax for packages enabled in 3.0: from .


Context managers enabled in 2.6 and 3.0: with/as

33, 35

Exception syntax changes in 3.0: raise, except/as, superclass

33, 34

Exception chaining in 3.0: raise e2 from e1


Reserved word changes in 2.6 and 3.0


New-style class cutover in 3.0


Property decorators in 2.6 and 3.0: @property


Descriptor use in 2.6 and 3.0

31, 38

Metaclass use in 2.6 and 3.0

31, 39

Abstract base classes support in 2.6 and 3.0


Specific Language Removals in 3.0

In addition to extensions, a number of language tools have been removed in 3.0 in an effort to clean up its design. Table P-2 summarizes the changes that impact this book, covered in various chapters of this edition. Many of the removals listed in Table P-2 have direct replacements, some of which are also available in 2.6 to support future migration to 3.0.

Table P-2. Removals in Python 3.0 that impact this book



Covered in Chapter(s)


imp.reload(M) (or exec)

3, 22

apply(f, ps, ks)

f(*ps, **ks)





X <> Y

X != Y









K in D (or D.get(key) != None)




3, 10

old input







open (and io module classes)


X.__next__, called by next(X)

14, 20, 29


X.__getitem__ passed a slice object

7, 29


X.__setitem__ passed a slice object

7, 29


functools.reduce (or loop code)

14, 19




exec open(filename)






print x, y

print(x, y)


print >> F, x, y

print(x, y, file=F)


print x, y,

print(x, y, end=' ')




7, 36

'bbb' for byte strings


7, 9, 36

raise E, V

raise E(V)

32, 33, 34

except E, X:

except E as X:

32, 33, 34

def f((a, b)):

def f(x): (a, b) = x

11, 18, 20


for line in file: (or X=iter(file))

13, 14

D.keys(), etc. as lists

list(D.keys()) (dictionary views)

8, 14

map(), range(), etc. as lists

list(map()), list(range()) (built-ins)


map(None, ...)

zip (or manual code to pad results)

13, 20

X=D.keys(); X.sort()

sorted(D) (or list(D.keys()))

4, 8, 14

cmp(x, y)

(x > y) - (x < y)



__lt__, __gt__, __eq__, etc.





Sort comparison functions

Use key=transform or reverse=True


Dictionary <, >, <=, >=

Compare sorted(D.items()) (or loop code)

8, 9


list (types isfor non-built-in names only)


__metaclass__ = M

class C(metaclass=M):

28, 31, 39


builtins (renamed)



tkinter (renamed)

18, 19, 29, 24, 30

sys.exc_type, exc_value

sys.exc_info()[0], [1]

34, 35



19, 38

__getattr__ run by built-ins

Redefine __X__ methods in wrapper classes

30, 37, 38

-t, Ėtt command-line switches

Inconsistent tabs/spaces use is always an error

10, 12

from ... *, within a function

May only appear at the top level of a file


import mod, in same package

from . import mod, package-relative form


class MyException:

class MyException(Exception):


exceptions module

Built-in scope, library manual


thread, Queue modules

_thread, queue (both renamed)


anydbm module

dbm (renamed)


cPickle module

_pickle (renamed, used automatically)



subprocess.Popen (os.popen retained)


String-based exceptions

Class-based exceptions (also required in 2.6)

32, 33, 34

String module functions

String object methods


Unbound methods

Functions (staticmethod to call via instance)

30, 31

Mixed type comparisons, sorts

Non-numeric mixed type comparisons are errors

5, 9

There are additional changes in Python 3.0 that are not listed in this table, simply because they donít affect this book. Changes in the standard library, for instance, might have a larger impact on applications-focused books like Programming Python than they do here; although most standard library functionality is still present, Python 3.0 takes further liberties with renaming modules, grouping them into packages, and so on. For a more comprehensive list of changes in 3.0, see the ďWhatís New in Python 3.0Ē document in Pythonís standard manual set.

If you are migrating from Python 2.X to Python 3.X, be sure to also see the 2to3 automatic code conversion script that is available with Python 3.0. It canít translate everything, but it does a reasonable job of converting the majority of 2.X code to run under 3.X. As I write this, a new 3to2 back-conversion project is also underway to translate Python 3.X code to run in 2.X environments. Either tool may prove useful if you must maintain code for both Python lines; see the Web for details.

Because this fourth edition is mostly a fairly straightforward update for 3.0 with a handful of new chapters, and because itís only been two years since the prior edition was published, the rest of this Preface is taken from the prior edition with only minor updating.

[end preview text]

[Home] Books Programs Blog Python Author Training Search Email ©M.Lutz