logo资料库

Think.Python.3rd.Edition.pdf

第1页 / 共421页
第2页 / 共421页
第3页 / 共421页
第4页 / 共421页
第5页 / 共421页
第6页 / 共421页
第7页 / 共421页
第8页 / 共421页
资料共421页,剩余部分请下载后查看
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
How to Think Like a Computer Scientist: Learning with Python 3 Documentation Release 3rd Edition Peter Wentworth, Jeffrey Elkner, Allen B. Downey and Chris Meyers August 12, 2012
CONTENTS 1 The way of the program . . . . . . . . . . . . . The Python programming language . . . . . . . . . . . . . . . . . . . . . . . 1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2 What is a program? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.3 What is debugging? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.4 . . Syntax errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Runtime errors . 1.5 . Semantic errors . . 1.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Experimental debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.7 1.8 Formal and natural languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The first program . 1.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.10 Comments 1.11 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.12 Exercises . . . . . . . . . . . . . . . . . . . 1 1 3 3 3 4 4 4 5 6 7 7 9 2 Variables, expressions and statements . . . . . . . . . . . 11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.1 Values and data types . 2.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 . 2.3 Variable names and keywords . . . . . . . . . . . . . . . . . . . . . . . . . . 14 . Statements . . 2.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.5 Evaluating expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.6 Operators and operands 2.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.8 Order of operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.9 Operations on strings . . . . 2.10 Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 . 2.11 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 . 2.12 The modulus operator . . 2.13 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.14 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Type converter functions . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Hello, little turtles! 25 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Instances — a herd of turtles . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 The for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Flow of Execution of the for loop . . . . . . . . . . . . . . . . . . . . . . . . 30 3.1 Our first turtle program . 3.2 3.3 3.4 . . . . . i
3.5 The loop simplifies our turtle program . . . . . . . . . . . . . . . . . . . . . . 31 3.6 A few more turtle methods and tricks . . . . . . . . . . . . . . . . . . . . . . 32 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 3.7 Glossary . 3.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 . . . . . . . . . . . . . 4 Functions . . . . . . . 39 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.1 Functions can call other functions . . . . . . . . . . . . . . . . . . . . . . . . 42 4.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Flow of execution . 4.3 . . . . . . . . . . . . . . . . . . . . . . . . 45 Functions that require arguments 4.4 Functions that return values . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 4.5 4.6 Variables and parameters are local . . . . . . . . . . . . . . . . . . . . . . . . 47 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 4.7 . . 4.8 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 4.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Turtles Revisited . . . . . . . . . . . . . . . . . . . . 5 Conditionals . . . . . . . . . 53 5.1 Boolean values and expressions . . . . . . . . . . . . . . . . . . . . . . . . . 53 5.2 Logical operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 5.3 Truth Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 . 5.4 Simplifying Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . 55 5.5 Conditional execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 5.6 Omitting the else clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 5.7 5.8 Nested conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 5.9 . 5.10 Logical opposites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 5.11 Type conversion . 5.12 A Turtle Bar Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 . . 5.13 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 . 5.14 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Chained conditionals . . . . The return statement . . . . . . . . . . . . . . . . . . . 6 Fruitful functions . . . . . Return values . . Program development 71 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 6.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 6.2 . 6.3 Debugging with print . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 . 6.4 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Boolean functions 6.5 . 6.6 Programming with style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 . 6.7 Unit testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 6.8 Glossary . . Exercises . . 6.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 . . . . . . . . . . . . . . . . . . . . . . . . . . . . Iteration 7.1 Assignment . . 7.2 Updating variables . 7.3 7.4 7.5 85 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 The for loop revisited . The while statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 . The Collatz 3n + 1 sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 . . 7 ii
. . . . . . . . Tracing a program . . Counting digits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 7.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 7.7 . . 7.8 Abbreviated assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 7.9 Help and meta-notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 7.10 Tables . . . 7.11 Two-dimensional tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 7.12 Encapsulation and generalization . . . . . . . . . . . . . . . . . . . . . . . . 96 7.13 More encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 . . 7.14 Local variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 7.15 The break statement . 7.16 Other flavours of loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 7.17 An example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 . 7.18 The continue statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 7.19 More generalization . 7.20 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 7.21 Paired Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 . 7.22 Nested Loops for Nested Data . . . . . . . . . . . . . . . . . . . . . . . . . . 104 7.23 Newton’s method for finding square roots . . . . . . . . . . . . . . . . . . . . 105 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 7.24 Algorithms . . . . 7.25 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 7.26 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Strings . . . . . . . . . . . . . . . . 113 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 8.1 A compound data type . . . . . . . . . . . . . . . . . . . . . . . 113 8.2 Working with strings as single things 8.3 Working with the parts of a string . . . . . . . . . . . . . . . . . . . . . . . . 115 Length . . 8.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Traversal and the for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 8.5 . . 8.6 Slices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 String comparison . 8.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Strings are immutable . 8.8 . 8.9 The in and not in operators . . . . . . . . . . . . . . . . . . . . . . . . . 119 8.10 A find function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 8.11 Looping and counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 8.12 Optional parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 8.13 The built-in find method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 8.14 The split method . 8.15 Cleaning up your strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 8.16 The string format method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 8.17 Summary . . . 8.18 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 8.19 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 . . . . . . . . . . . . . . . . . . . . . . . . 9 Tuples 133 Tuples are used for grouping data . . . . . . . . . . . . . . . . . . . . . . . . 133 . Tuple assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Tuples as return values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Composability of Data Structures . . . . . . . . . . . . . . . . . . . . . . . . 135 . . . . 9.1 9.2 9.3 9.4 iii
9.5 Glossary . Exercises . 9.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 . . 10 Event-Driven Programming . . 137 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 10.1 Keypress events 10.2 Mouse events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 10.3 Automatic events from a timer . . . . . . . . . . . . . . . . . . . . . . . . . . 140 10.4 An example: state machines . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 . 10.5 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 10.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 . . . . . . . . . . . . . . . . . . . . . 11 Lists . . . . . . . . . . . . . . . . . . . . . 145 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 . 11.1 List values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 . 11.2 Accessing elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 . 11.3 List length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 . 11.4 List membership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 . . 11.5 List operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 . 11.6 List slices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 . 11.7 Lists are mutable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 11.8 List deletion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 . 11.9 Objects and references . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 . 11.10 Aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 . . 11.11 Cloning lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 . 11.12 Lists and for loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 11.13 List parameters . . . 11.14 List methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 . . . 11.15 Pure functions and modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 11.16 Functions that produce lists 11.17 Strings and lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 . 11.18 list and range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 . . 11.19 Nested lists . . 11.20 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 . 11.21 Glossary . . 11.22 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Modules . . . . 163 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 12.1 Random numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 12.2 The time module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 12.3 The math module . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 12.4 Creating your own modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 12.5 Namespaces 12.6 Scope and lookup rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 12.7 Attributes and the dot operator . . . . . . . . . . . . . . . . . . . . . . . . . . 171 12.8 Three import statement variants . . . . . . . . . . . . . . . . . . . . . . . . 172 12.9 Turn your unit tester into a module . . . . . . . . . . . . . . . . . . . . . . . 172 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 12.10 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 . 12.11 Exercises . . . . . . . . . . . . . . . . . 13 Files iv 179
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 13.1 About files . 13.2 Writing our first file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 13.3 Reading a file line-at-a-time . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 13.4 Turning a file into a list of lines . . . . . . . . . . . . . . . . . . . . . . . . . 181 13.5 Reading the whole file at once . . . . . . . . . . . . . . . . . . . . . . . . . . 182 13.6 Working with binary files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 . . 13.7 An example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 13.8 Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 13.9 What about fetching something from the web? . . . . . . . . . . . . . . . . . 184 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 13.10 Glossary . 13.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 . . . . . . . . . . . . . . . . . . . . . 14 List Algorithms . 187 14.1 Test-driven development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 14.2 The linear search algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 14.3 A more realistic problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 14.4 Binary Search . 14.5 Removing adjacent duplicates from a list . . . . . . . . . . . . . . . . . . . . 195 14.6 Merging sorted lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 14.7 Alice in Wonderland, again! . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 14.8 Eight Queens puzzle, part 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 14.9 Eight Queens puzzle, part 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 . 14.10 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 14.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 . . . . . . . . . . . . . . . . . . . . 15 Classes and Objects — the Basics . . . . . . . 209 15.1 Object-oriented programming . . . . . . . . . . . . . . . . . . . . . . . . . . 209 15.2 User-defined compound data types . . . . . . . . . . . . . . . . . . . . . . . . 209 15.3 Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 15.4 Improving our initializer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 15.5 Adding other methods to our class . . . . . . . . . . . . . . . . . . . . . . . . 213 15.6 Instances as arguments and parameters . . . . . . . . . . . . . . . . . . . . . 214 15.7 Converting an instance to a string . . . . . . . . . . . . . . . . . . . . . . . . 215 15.8 Instances as return values . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 15.9 A change of perspective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 15.10 Objects can have state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 . . . 15.11 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 15.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 . . . . . . . . . . . . 16 Classes and Objects — Digging a little deeper 16.1 Rectangles . . . . 16.2 Objects are mutable 16.3 Sameness . . . . 16.4 Copying . . . 16.5 Glossary . . 16.6 Exercises . . . . . . . . . . . 17 PyGame 17.1 The game loop . . . . . . . . . . . . . . . . . . . . . . 221 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 227 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 v
. . . . . . . . . . . . . . . . . 17.2 Displaying images and text . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 17.3 Drawing a board for the N queens puzzle . . . . . . . . . . . . . . . . . . . . 232 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 17.4 Sprites 17.5 Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 17.6 A wave of animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 17.7 Aliens - a case study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 17.8 Reflections . . . 17.9 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 17.10 Exercises . . . . . . . . . . . . . . . . . . 18 Recursion . . . 249 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 18.1 Drawing Fractals . 18.2 Recursive data structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 18.3 Processing recursive number lists . . . . . . . . . . . . . . . . . . . . . . . . 252 18.4 Case study: Fibonacci numbers . . . . . . . . . . . . . . . . . . . . . . . . . 254 18.5 Example with recursive directories and files . . . . . . . . . . . . . . . . . . . 255 . 18.6 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 18.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 . . . . . . . . . . . . . . 19 Exceptions . 261 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 19.1 Catching exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 19.2 Raising our own exceptions 19.3 Revisiting an earlier example . . . . . . . . . . . . . . . . . . . . . . . . . . 264 19.4 The finally clause of the try statement . . . . . . . . . . . . . . . . . . . 264 . 19.5 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 . 19.6 Exercises . . . . . . . . . . . . . . 20 Dictionaries 20.1 Dictionary operations 20.2 Dictionary methods . 20.3 Aliasing and copying . . 20.4 Sparse matrices . . 20.5 Memoization . . . . . 20.6 Counting letters . . . 20.7 Glossary . . . . . 20.8 Exercises . . . . . . . . . . 267 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 . . . . . . . . 21 Even more OOP . . . . . . . . . . . . . . . . . . 277 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 21.1 MyTime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 21.2 Pure functions 21.3 Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 . 21.4 Converting increment to a method . . . . . . . . . . . . . . . . . . . . . . 279 . . 21.5 An “Aha!” insight . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 . 21.6 Generalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 21.7 Another example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 21.8 Operator overloading . . 21.9 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 . . 21.10 Glossary . . . 21.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 . . . . . . . . . . . . . . . . vi
分享到:
收藏