Introduction
to
Computation
and
Programming
Using
Python
Revised and
Expanded
Edition
John
V.
Guttag
The MIT
PressCambridge,
Massachusetts
London,
England
CONTENTS
PREFACE xiii
ACKNOWLEDGMENTS xv
1 GETTING STARTED 1 2 INTRODUCTION TO PYTHON 7 2.1 The Basic Elements of
Python
82.1.1
Objects, Expressions,
and NumericalTypes
9 2.1.2 Variables andAssignment
112.1.3 IDLE 13
2.2
Branching Programs
142.3
Strings
andInput
162.3.1
Input
182.4 Iteration 18
3 SOME SIMPLE NUMERICAL PROGRAMS 21 3.1 Exhaustive Enumeration 21
3.2 For
Loops
233.3
Approximate
Solutions and Bisection Search 25 3.4 A Few Words AboutUsing
Floats 293.5
Newton-Raphson
324
FUNCTIONS, SCOPING,
and ABSTRACTION 344.1 Functions and
Scoping
354.1.1 Function Definitions 35 4.1.2
Keyword Arguments
and DefaultValues 364.1.3
Scoping
37 4.2Specifications
41 4.3 Recursion 44 4.3.1 Fibonacci Numbers 45 4.3.2 Palindromes 48 4.4 Global Variables 50 4.5 Modules 51 4.6 Files 535 STRUCTURED TYPES, MUTABILITY, AND HIGHER-ORDER FUNCTIONS.. 56
5.1
Tuples
565.1.1
Sequences
andMultiple Assignment
575.2 Lists and
Mutability
585.2.1
Cloning
635.2.2 List
Comprehension
63 5.3 Functions asObjects
645.4
Strings,
Tuples,
and Lists 665.5 Dictionaries 67
6 TESTING AND DEBUGGING 70
6.1
Testing
70 6.1.1 Black-BoxTesting
71 6.1.2 Glass-BoxTesting
73 6.1.3Conducting
Tests 74 6.2Debugging
76 6.2.1Learning
toDebug
78 6.2.2Designing
theExperiment
79 6.2.3 When theGoing
GetsTough
816.2.4 And When You Have Found "The"
Bug
82 7 EXCEPTIONS AND ASSERTIONS 847.1
Handling Exceptions
847.2
Exceptions
as a Control Flow Mechanism 877.3 Assertions 90
8 CLASSES AND OBJECT-ORIENTED PROGRAMMING 91 8.1 Abstract Data
Types
and Classes 91 8.1.1Designing Programs Using
Abstract DataTypes
968.1.2
Using
Classes toKeep
Track of Students andFaculty
96 8.2 Inheritance 99 8.2.1Multiple
Levels ofInheritance 1018.2.2 The Substitution
Principle
1028.3
Encapsulation
and InformationHiding
1038.3.1 Generators 106
ix
9 A SIMPLISTIC INTRODUCTION TO ALGORITHMIC COMPLEXITY 113
9.1
Thinking
AboutComputational Complexity
1139.2
Asymptotic
Notation 1169.3 Some
Important
Complexity
Classes 1189.3.1 Constant
Complexity
118 9.3.2Logarithmic
Complexity
118 9.3.3 LinearComplexity
119 9.3.4Log-Linear
Complexity
120 9.3.5Polynomial Complexity
120 9.3.6Exponential
Complexity
121 9.3.7Comparisons
ofComplexity
Classes 123 10 SOME SIMPLE ALGORITHMS AND DATA STRUCTURES 12510.1 Search
Algorithms
12610.1.1 Linear Search and
Using
Indirection to Access Elements 126 10.1.2Binary
Search andExploiting Assumptions
128 10.2Sorting
Algorithms
13110.2.1
Merge
Sort 13210.2.2
Exploiting
Functions asParameters 13510.2.3
Sorting
inPython
13610.3 Hash Tables 137
11 PLOTTING AND MORE ABOUT CLASSES 141 11.1
Plotting
Using PyLab
141 11.2Plotting
Mortgages,
an ExtendedExample
14612 STOCHASTIC
PROGRAMS, PROBABILITY,
AND STATISTICS 15212.1 Stochastic
Programs
15312.2 Inferential Statistics and Simulation 155 12.3 Distributions 166
12.3.1 Normal Distributions and Confidence Levels 168 12.3.2 UniformDistributions 170 12.3.3
Exponential
and Geometric Distributions 171 12.3.4 Benford's Distribution 17312.4 How Often Does the Better Team Win? 174
X
13 RANDOM WALKS AND MORE ABOUT DATA VISUALIZATION 179 13.1 The Drunkard's Walk 179 13.2 Biased Random Walks 186
13.3 Treacherous Fields 191 14 MONTE CARLO SIMULATION 193
14.1 Pascal's Problem 194
14.2 Pass or Don't Pass? 195
14.3
Using
TableLookup
toImprove
Performance 19914.4
Finding
* 20014.5 Some
Closing
Remarks About Simulation Models 204 15 UNDERSTANDING EXPERIMENTAL DATA 207 15.1 The Behavior ofSprings
207 15.1.1Using
LinearRegression
to Find a Fit 21015.2 The Behavior of
Projectiles
21415.2.1 Coefficient ofDetermination 216 15.2.2
Using
aComputational
Model 21715.3
Fitting Exponentially
Distributed Data 21815.4 When
Theory
IsMissing
22116 LIES. DAMNED LIES, AND STATISTICS 222
16.1
Garbage
InGarbage
Out(GIGO)
222 16.2 Pictures CanBeDeceiving
22316.3 dimHoc
Ergo Propter
Hoc 22516.4 Statistical Measures Don't Tell the Whole
Story
22616.5
Sampling
Bias 22816.6 Context Matters 229
16.7 Beware of
Extrapolation
229 16.8 The TexasSharpshooter Fallacy
23016.9
Percentages
Can Confuse 232 16.10 Just Beware 23317 KNAPSACK AND GRAPH OPTIMIZATION PROBLEMS 234
17.1
Knapsack
Problems 234 17.1.1Greedy Algorithms
235 17.1.2 AnOptimal
Solution to the0/1 Knapsack
Problem 23817.2
Graph Optimization
Problems 240 17.2.1 Some ClassicGraph-Theoretic
Problems 24417.2.2 The
Spread
of Disease and Min Cut 245 17.2.3 Shortest Path:Depth-First
Search and Breadth-First Search.... 24618 DYNAMIC PROGRAMMING 252
18.1 Fibonacci
Sequences,
Revisited 25218.2
Dynamic Programming
and the0/1
Knapsack
Problem 254 18.3Dynamic
Programming
andDivide-and-Conquer
26119 A
QUICK
LOOK AT MACHINE LEARNING 26219.1 Feature Vectors 264 19.2 Distance Metrics 266
19.3
Clustering
27019.4
Types
Example
and Cluster 27219.5 K-means
Clustering
274 19.6 A ContrivedExample
276 19.7 A Less ContrivedExample
28019.8
Wrapping Up
286