Dive Into Python
20 May 2004
Copyright (c) 2000, 2001, 2002, 2003, 2004 Mark Pilgrim (mailto:
Mark@diveintopython. org)
Else, you may not have the latest version.
Permission is granted to copy, distribute, and/or modify this document under
The terms of the GNU Free Documentation License, Version 1.1 or any later
Version published by the Free Software Foundation; with no Invariant Sections,
No Front-Cover Texts, and no Back-Cover Texts. A copy of the license is
Included in AppendixβG, GNU Free Documentation License.
The example programs in this book are free software; you can redistribute and/
Or modify them under the terms of the Python license as published by the Python
Software Foundation. A copy of the license is included in AppendixβH, Python
License.
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Table of Contents
ββ*β1. Installing Python
ββ+β1.1. Which Python is right for you?
ββ+β1.2. Python on Windows
ββ+β1.3. Python on Mac OS X
ββ+β1.4. Python on Mac OS 9
ββ+β1.5. Python on RedHat Linux
ββ+β1.6. Python on Debian GNU/Linux
ββ+β1.7. Python Installation from Source
ββ+β1.8. The Interactive Shell
ββ+β1.9. Summary
ββ*β2. Your First Python Program
ββ+β2.1. Diving in
ββ+β2.2. Declaring Functions
ββoβ2.2.1. How Python’s Datatypes Compare to Other Programming
Languages
ββ+β2.3. Documenting Functions
ββ+β2.4. Everything Is an Object
ββoβ2.4.1. The Import Search Path
ββoβ2.4.2.
What’s an Object?
ββ+β2.5. Indenting Code
ββ+β2.6. Testing Modules
ββ*β3. Native Datatypes
ββ+β3.1. Introducing Dictionaries
ββoβ3.1.1. Defining Dictionaries
ββoβ3.1.2. Modifying Dictionaries
ββoβ3.1.3. Deleting Items From Dictionaries
ββ+β3.2. Introducing Lists
ββoβ3.2.1. Defining Lists
ββoβ3.2.2. Adding Elements to Lists
ββoβ3.2.3. Searching Lists
ββoβ3.2.4. Deleting List Elements
ββoβ3.2.5. Using List Operators
ββ+β3.3. Introducing Tuples
ββ+β3.4. Declaring variables
ββoβ3.4.1. Referencing Variables
ββoβ3.4.2. Assigning Multiple Values at Once
ββ+β3.5. Formatting Strings
ββ+β3.6. Mapping Lists
ββ+β3.7. Joining Lists and Splitting Strings
ββoβ3.7.1. Historical Note on String Methods
ββ+β3.8. Summary
ββ*β4. The Power Of Introspection
ββ+β4.1. Diving In
ββ+β4.2. Using Optional and Named Arguments
ββ+β4.3. Using type, str, dir, and Other Built-In Functions
ββoβ4.3.1. The type Function
ββoβ4.3.2. The str Function
ββoβ4.3.3. Built-In Functions
ββ+β4.4. Getting Object References With getattr
ββoβ4.4.1. getattr with Modules
ββoβ4.4.2. getattr As a Dispatcher
ββ+β4.5. Filtering Lists
ββ+β4.6. The Peculiar Nature of and and or
ββoβ4.6.1. Using the and-or Trick
ββ+β4.7. Using lambda Functions
ββoβ4.7.1. Real-World lambda Functions
ββ+β4.8. Putting It All Together
ββ+β4.9. Summary
ββ*β5. Objects and Object-Orientation
ββ+β5.1. Diving In
ββ+β5.2.