Index 249 redundancy, 7 reference, 181 aliasing, 92 embedded, 136, 181, 190 regular expression, 194 removing cards, 166 repetition list, 87 return statement, 42, 228 return value, 23, 35, 49, 60, 134 tuple, 99 role variable, 186 root node, 207, 219 row, 95 rules of precedence, 17, 21 runtime error, 5, 9, 45, 74, 77, 85, 98, 109, 111, 118, 121, 221, 225 safe language, 5 sameness, 131 scaffolding, 50, 60 scalar multiplication, 154, 158 script, 9 semantic error, 5, 9, 99, 221, 227 semantics, 5, 9 sequence, 83, 96 shallow copy, 137 shallow equality, 132, 137 shuffle, 165 sibling node, 219 single<strong>to</strong>n, 187, 188, 190 slice, 76, 81, 88 source code, 9 split function, 95 Stack, 192 stack, 192 stack diagram, 32, 35, 44 state diagram, 12, 21 statement, 21 assignment, 12, 61 block, 39 break, 119, 126 compound, 39 conditional, 47 continue, 120, 126 except, 124 pass, 39 print, 8, 9, 226 return, 42, 228 try, 124 while, 62 straight flush, 172 string, 11 immutable, 77 length, 74 slice, 76 string comparison, 76 string module, 79, 80 string operation, 18 subclass, 169, 172, 179 subexpression, 215 suit, 159 sum, 214 swap, 166 syntax, 4, 9, 222 syntax error, 4, 9, 221 tab, 72 table, 64 two-dimensional, 66 temporary variable, 50, 60, 228 text file, 119, 126 theorem fundamental ambiguity, 186 <strong>to</strong>ken, 9, 194, 197, 212 traceback, 33, 35, 45, 125, 225 traversal, 74, 78, 86, 174 list, 85 traverse, 81, 183, 184, 204, 209, 210 tree, 207 empty, 208 expression, 209, 212 traversal, 209, 210 tree node, 207 try, 126
250 Index try statement, 124 tuple, 97, 99, 105 tuple assignment, 98, 105, 166 Turing Thesis, 55 Turing, Alan, 55 type, 11, 21 dict, 107 file, 117 float, 11 int, 11 list, 83 long, 113 str, 11 tuple, 97 type checking, 58 type coercion, 24, 113 type conversion, 24 TypeError, 225 unary negation, 237 unary opera<strong>to</strong>r, 236 underscore character, 13 uppercase, 80 user-defined data type, 129 value, 11, 21, 91 tuple, 99 variable, 12, 21 local, 31, 69 loop, 171 roles, 186 temporary, 50, 60, 228 veneer, 193, 206 while statement, 62 whitespace, 80, 81 wrapper, 190 wrapper function, 187
- Page 2 and 3:
How to Think Like a Computer Scient
- Page 4 and 5:
How to Think Like a Computer Scient
- Page 6 and 7:
Foreword By David Beazley As an edu
- Page 8 and 9:
Preface By Jeff Elkner This book ow
- Page 10 and 11:
make instant changes whenever someo
- Page 12 and 13:
call (type) out its name.” Parame
- Page 14 and 15:
Contributor List To paraphrase the
- Page 16 and 17:
• Paul Sleigh found an error in C
- Page 18 and 19:
xvii • Roger Sperberg pointed out
- Page 20 and 21:
Contents Foreword v Preface vii Con
- Page 22 and 23:
Contents xxi 4.7 Nested conditional
- Page 24 and 25:
Contents xxiii 8.15 Matrices . . .
- Page 26 and 27:
Contents xxv 14.4 A more complicate
- Page 28 and 29:
Contents xxvii 19.4 Improved Linked
- Page 30 and 31:
Chapter 1 The way of the program Th
- Page 32 and 33:
1.2 What is a program? 3 $ python P
- Page 34 and 35:
1.3 What is debugging? 5 1.3.2 Runt
- Page 36 and 37:
1.4 Formal and natural languages 7
- Page 38 and 39:
1.6 Glossary 9 source code: A progr
- Page 40 and 41:
Chapter 2 Variables, expressions an
- Page 42 and 43:
2.3 Variable names and keywords 13
- Page 44 and 45:
2.4 Statements 15 >>> 76trombones =
- Page 46 and 47:
2.6 Operators and operands 17 2.6 O
- Page 48 and 49:
2.9 Composition 19 On one hand, thi
- Page 50 and 51:
2.11 Glossary 21 state diagram: A g
- Page 52 and 53:
Chapter 3 Functions 3.1 Function ca
- Page 54 and 55:
3.4 Math functions 25 >>> minute =
- Page 56 and 57:
3.6 Adding new functions 27 problem
- Page 58 and 59:
3.7 Definitions and use 29 3.7 Defi
- Page 60 and 61:
3.10 Variables and parameters are l
- Page 62 and 63:
3.12 Functions with results 33 The
- Page 64 and 65:
3.13 Glossary 35 local variable: A
- Page 66 and 67:
Chapter 4 Conditionals and recursio
- Page 68 and 69:
4.4 Conditional execution 39 In gen
- Page 70 and 71:
4.7 Nested conditionals 41 Each con
- Page 72 and 73:
4.9 Recursion 43 countdown expects
- Page 74 and 75:
4.11 Infinite recursion 45 4.11 Inf
- Page 76 and 77:
4.13 Glossary 47 block: A group of
- Page 78 and 79:
Chapter 5 Fruitful functions 5.1 Re
- Page 80 and 81:
5.2 Program development 51 avoid lo
- Page 82 and 83:
5.3 Composition 53 3. Once the prog
- Page 84 and 85:
5.5 More recursion 55 But the extra
- Page 86 and 87:
5.6 Leap of faith 57 __main__ facto
- Page 88 and 89:
5.8 Checking types 59 >>> factorial
- Page 90 and 91:
Chapter 6 Iteration 6.1 Multiple as
- Page 92 and 93:
6.2 The while statement 63 1. Evalu
- Page 94 and 95:
6.3 Tables 65 1.0 0.0 2.0 0.6931471
- Page 96 and 97:
6.5 Encapsulation and generalizatio
- Page 98 and 99:
6.7 Local variables 69 6.7 Local va
- Page 100 and 101:
6.9 Functions 71 the function is ca
- Page 102 and 103:
Chapter 7 Strings 7.1 A compound da
- Page 104 and 105:
7.3 Traversal and the for loop 75 T
- Page 106 and 107:
7.6 Strings are immutable 77 Other
- Page 108 and 109:
7.9 The string module 79 As an exer
- Page 110 and 111:
7.11 Glossary 81 There are other us
- Page 112 and 113:
Chapter 8 Lists A list is an ordere
- Page 114 and 115:
8.3 List length 85 If you try to re
- Page 116 and 117:
8.6 List operations 87 for VARIABLE
- Page 118 and 119:
8.9 List deletion 89 >>> list = [
- Page 120 and 121:
8.10 Objects and values 91 8.10 Obj
- Page 122 and 123:
8.13 List parameters 93 >>> b[0] =
- Page 124 and 125:
8.16 Strings and lists 95 might be
- Page 126 and 127:
Chapter 9 Tuples 9.1 Mutability and
- Page 128 and 129:
9.3 Tuples as return values 99 9.3
- Page 130 and 131:
9.6 Counting 101 >>> randomList(8)
- Page 132 and 133:
9.7 Many buckets 103 bucketWidth =
- Page 134 and 135:
9.9 Glossary 105 9.9 Glossary Assig
- Page 136 and 137:
Chapter 10 Dictionaries The compoun
- Page 138 and 139:
10.2 Dictionary methods 109 10.2 Di
- Page 140 and 141:
10.5 Hints 111 An alternative is to
- Page 142 and 143:
10.6 Long integers 113 Using this v
- Page 144 and 145:
10.8 Glossary 115 overflow: A numer
- Page 146 and 147:
Chapter 11 Files and exceptions Whi
- Page 148 and 149:
11.1 Text files 119 The following f
- Page 150 and 151:
11.2 Writing variables 121 the str
- Page 152 and 153:
11.3 Directories 123 11.3 Directori
- Page 154 and 155:
11.5 Exceptions 125 Or trying to op
- Page 156 and 157:
11.6 Glossary 127 format sequence:
- Page 158 and 159:
Chapter 12 Classes and objects 12.1
- Page 160 and 161:
12.3 Instances as arguments 131 con
- Page 162 and 163:
12.5 Rectangles 133 >>> p1 = Point(
- Page 164 and 165:
12.8 Copying 135 The variables dwid
- Page 166 and 167:
12.9 Glossary 137 12.9 Glossary cla
- Page 168 and 169:
Chapter 13 Classes and functions 13
- Page 170 and 171:
13.3 Modifiers 141 The output of th
- Page 172 and 173:
13.5 Prototype development versus p
- Page 174 and 175:
13.8 Glossary 145 Similarly, the te
- Page 176 and 177:
Chapter 14 Classes and methods 14.1
- Page 178 and 179:
14.3 Another example 149 class Time
- Page 180 and 181:
14.5 Optional arguments 151 if done
- Page 182 and 183:
14.7 Points revisited 153 >>> curre
- Page 184 and 185:
14.9 Polymorphism 155 def __mul__(s
- Page 186 and 187:
14.10 Glossary 157 Of course, we in
- Page 188 and 189:
Chapter 15 Sets of objects 15.1 Com
- Page 190 and 191:
15.3 Class attributes and the str m
- Page 192 and 193:
15.5 Decks 163 def __cmp__(self, ot
- Page 194 and 195:
15.7 Shuffling the deck 165 >>> dec
- Page 196 and 197:
15.9 Glossary 167 class Deck: ... d
- Page 198 and 199:
Chapter 16 Inheritance 16.1 Inherit
- Page 200 and 201:
16.3 Dealing cards 171 16.3 Dealing
- Page 202 and 203:
16.6 OldMaidHand class 173 class Ca
- Page 204 and 205:
16.7 OldMaidGame class 175 Hand fra
- Page 206 and 207:
16.7 OldMaidGame class 177 class Ol
- Page 208 and 209:
16.8 Glossary 179 Hand Allen picked
- Page 210 and 211:
Chapter 17 Linked lists 17.1 Embedd
- Page 212 and 213:
17.3 Lists as collections 183 node1
- Page 214 and 215:
17.5 Infinite lists 185 We invoke t
- Page 216 and 217:
17.8 Wrappers and helpers 187 def r
- Page 218 and 219:
17.10 Invariants 189 tail = self.ne
- Page 220 and 221:
Chapter 18 Stacks 18.1 Abstract dat
- Page 222 and 223:
18.4 Pushing and popping 193 def po
- Page 224 and 225:
18.7 Evaluating postfix 195 Python
- Page 226 and 227:
18.9 Glossary 197 18.9 Glossary abs
- Page 228 and 229: Chapter 19 Queues This chapter pres
- Page 230 and 231: 19.3 Performance characteristics 20
- Page 232 and 233: 19.5 Priority queue 203 and it is m
- Page 234 and 235: 19.6 The Golfer class 205 19.6 The
- Page 236 and 237: Chapter 20 Trees Like linked lists,
- Page 238 and 239: 20.2 Traversing trees 209 20.2 Trav
- Page 240 and 241: 20.4 Tree traversal 211 def printTr
- Page 242 and 243: 20.5 Building an expression tree 21
- Page 244 and 245: 20.5 Building an expression tree 21
- Page 246 and 247: 20.7 The animal tree 217 Are you th
- Page 248 and 249: 20.8 Glossary 219 def yes(ques): fr
- Page 250 and 251: Appendix A Debugging Different kind
- Page 252 and 253: A.2 Runtime errors 223 A.1.1 I can
- Page 254 and 255: A.2 Runtime errors 225 If there is
- Page 256 and 257: A.3 Semantic errors 227 A.3 Semanti
- Page 258 and 259: A.3 Semantic errors 229 return self
- Page 260 and 261: Appendix B Creating a new data type
- Page 262 and 263: B.1 Fraction multiplication 233 >>>
- Page 264 and 265: B.4 Comparing fractions 235 This re
- Page 266 and 267: B.6 Glossary 237 reduce: To change
- Page 268 and 269: Appendix C Recommendations for furt
- Page 270 and 271: C.2 Recommended general computer sc
- Page 272 and 273: Index Make Way for Ducklings, 75 Py
- Page 274 and 275: Index 245 element, 83, 96 embedded
- Page 276 and 277: Index 247 well-formed, 189 list del