The way of the program
The Python programming language
What is a program?
What is debugging?
Syntax errors
Runtime errors
Semantic errors
Experimental debugging
Formal and natural languages
The first program
Comments
Glossary
Exercises
Variables, expressions and statements
Values and data types
Variables
Variable names and keywords
Statements
Evaluating expressions
Operators and operands
Type converter functions
Order of operations
Operations on strings
Input
Composition
The modulus operator
Glossary
Exercises
Hello, little turtles!
Our first turtle program
Instances — a herd of turtles
The for loop
Flow of Execution of the for loop
The loop simplifies our turtle program
A few more turtle methods and tricks
Glossary
Exercises
Functions
Functions
Functions can call other functions
Flow of execution
Functions that require arguments
Functions that return values
Variables and parameters are local
Turtles Revisited
Glossary
Exercises
Conditionals
Boolean values and expressions
Logical operators
Truth Tables
Simplifying Boolean Expressions
Conditional execution
Omitting the else clause
Chained conditionals
Nested conditionals
The return statement
Logical opposites
Type conversion
A Turtle Bar Chart
Glossary
Exercises
Fruitful functions
Return values
Program development
Debugging with print
Composition
Boolean functions
Programming with style
Unit testing
Glossary
Exercises
Iteration
Assignment
Updating variables
The for loop revisited
The while statement
The Collatz 3n + 1 sequence
Tracing a program
Counting digits
Abbreviated assignment
Help and meta-notation
Tables
Two-dimensional tables
Encapsulation and generalization
More encapsulation
Local variables
The break statement
Other flavours of loops
An example
The continue statement
More generalization
Functions
Paired Data
Nested Loops for Nested Data
Newton's method for finding square roots
Algorithms
Glossary
Exercises
Strings
A compound data type
Working with strings as single things
Working with the parts of a string
Length
Traversal and the for loop
Slices
String comparison
Strings are immutable
The in and not in operators
A find function
Looping and counting
Optional parameters
The built-in find method
The split method
Cleaning up your strings
The string format method
Summary
Glossary
Exercises
Tuples
Tuples are used for grouping data
Tuple assignment
Tuples as return values
Composability of Data Structures
Glossary
Exercises
Event-Driven Programming
Keypress events
Mouse events
Automatic events from a timer
An example: state machines
Glossary
Exercises
Lists
List values
Accessing elements
List length
List membership
List operations
List slices
Lists are mutable
List deletion
Objects and references
Aliasing
Cloning lists
Lists and for loops
List parameters
List methods
Pure functions and modifiers
Functions that produce lists
Strings and lists
list and range
Nested lists
Matrices
Glossary
Exercises
Modules
Random numbers
The time module
The math module
Creating your own modules
Namespaces
Scope and lookup rules
Attributes and the dot operator
Three import statement variants
Turn your unit tester into a module
Glossary
Exercises
Files
About files
Writing our first file
Reading a file line-at-a-time
Turning a file into a list of lines
Reading the whole file at once
Working with binary files
An example
Directories
What about fetching something from the web?
Glossary
Exercises
List Algorithms
Test-driven development
The linear search algorithm
A more realistic problem
Binary Search
Removing adjacent duplicates from a list
Merging sorted lists
Alice in Wonderland, again!
Eight Queens puzzle, part 1
Eight Queens puzzle, part 2
Glossary
Exercises
Classes and Objects — the Basics
Object-oriented programming
User-defined compound data types
Attributes
Improving our initializer
Adding other methods to our class
Instances as arguments and parameters
Converting an instance to a string
Instances as return values
A change of perspective
Objects can have state
Glossary
Exercises
Classes and Objects — Digging a little deeper
Rectangles
Objects are mutable
Sameness
Copying
Glossary
Exercises
PyGame
The game loop
Displaying images and text
Drawing a board for the N queens puzzle
Sprites
Events
A wave of animation
Aliens - a case study
Reflections
Glossary
Exercises
Recursion
Drawing Fractals
Recursive data structures
Processing recursive number lists
Case study: Fibonacci numbers
Example with recursive directories and files
Glossary
Exercises
Exceptions
Catching exceptions
Raising our own exceptions
Revisiting an earlier example
The finally clause of the try statement
Glossary
Exercises
Dictionaries
Dictionary operations
Dictionary methods
Aliasing and copying
Sparse matrices
Memoization
Counting letters
Glossary
Exercises
Even more OOP
MyTime
Pure functions
Modifiers
Converting increment to a method
An ``Aha!'' insight
Generalization
Another example
Operator overloading
Polymorphism
Glossary
Exercises
Collections of objects
Composition
Card objects
Class attributes and the __str__ method
Comparing cards
Decks
Printing the deck
Shuffling the deck
Removing and dealing cards
Glossary
Exercises
Inheritance
Inheritance
A hand of cards
Dealing cards
Printing a Hand
The CardGame class
OldMaidHand class
OldMaidGame class
Glossary
Exercises
Linked lists
Embedded references
The Node class
Lists as collections
Lists and recursion
Infinite lists
The fundamental ambiguity theorem
Modifying lists
Wrappers and helpers
The LinkedList class
Invariants
Glossary
Exercises
Stacks
Abstract data types
The Stack ADT
Implementing stacks with Python lists
Pushing and popping
Using a stack to evaluate postfix
Parsing
Evaluating postfix
Clients and providers
Glossary
Exercises
Queues
The Queue ADT
Linked Queue
Performance characteristics
Improved Linked Queue
Priority queue
The Golfer class
Glossary
Exercises
Trees
Building trees
Traversing trees
Expression trees
Tree traversal
Building an expression tree
Handling errors
The animal tree
Glossary
Exercises
Debugging
Syntax errors
I can't get my program to run no matter what I do.
Runtime errors
My program does absolutely nothing.
My program hangs.
Infinite Loop
Infinite Recursion
Flow of Execution
When I run the program I get an exception.
I added so many print statements I get inundated with output.
Semantic errors
My program doesn't work.
I've got a big hairy expression and it doesn't do what I expect.
I've got a function or method that doesn't return what I expect.
I'm really, really stuck and I need help.
No, I really need help.
An odds-and-ends Workbook
The Five Strands of Proficiency
Sending Email
Write your own Web Server
Using a Database
Configuring Ubuntu for Python Development
Vim
$HOME environment
Making a Python script executable and runnable from anywhere
Customizing and Contributing to the Book
Getting the Source
Making the HTML Version
Some Tips, Tricks, and Common Errors
Functions
String handling
Looping and lists
GNU Free Documentation License
0. PREAMBLE
1. APPLICABILITY AND DEFINITIONS
2. VERBATIM COPYING
3. COPYING IN QUANTITY
4. MODIFICATIONS
5. COMBINING DOCUMENTS
6. COLLECTIONS OF DOCUMENTS
7. AGGREGATION WITH INDEPENDENT WORKS
8. TRANSLATION
9. TERMINATION
10. FUTURE REVISIONS OF THIS LICENSE
11. RELICENSING
ADDENDUM: How to use this License for your documents
Index