206 Indexeval function, 69evaluate, 13event, 192event handler, 189event loop, 181, 192Event object, 189event string,189event-driven programming, 182, 191, 192exception, 3, 7, 15, 195, 198AttributeError,150, 199IndexError, 72, 78, 90, 199IOError,138KeyError, 104, 199NameError, 23, 198OverflowError, 46RuntimeError, 44SyntaxError, 19TypeError, 71, 74, 108, 116, 117, 136, 161,198UnboundLocalError, 111ValueError, 45, 107, 116exception, catching, 138executable, 2, 7exercise, secret, 143exists function, 137experimental debugging, 4, 133expression, 12, 13, 16bigand hairy, 200boolean, 39, 46extend method, 92factorial function, 56, 58False special value, 39Fermat’s Last Theorem, 47fibonacci function, 57, 109file, 135compression, 141permission, 138reading and writing,135fileobject, 81, 86filename, 137filter pattern, 93, 100find function, 74flag, 110, 113float function, 17float type, 9floating-point, 15, 67floating-point division, 12floor division, 12, 16, 46flow of execution, 21, 26, 58, 59, 64, 177, 191,198flower, 37folder, 137for loop, 30, 72, 91, 119formal language, 4, 8format operator, 136, 143, 198format sequence, 136, 143format string,136, 143frabjous, 56frame, 23, 26, 43, 57, 109Frame widget, 186Free Documentation License, GNU, v, vifrequency, 105letter,123word, 125, 134fruitfulfunction, 24, 26frustration,201function, 19, 25, 159abs, 52ack, 61arc, 31choice, 126circle, 31cmp, 172compare, 52deepcopy, 150dict, 103enumerate, 119eval, 69exists, 137factorial, 56fibonacci, 57, 109find, 74float, 17getattr, 166getcwd, 137hasattr, 150, 166int, 17isinstance, 58, 164len, 26, 72, 104list,95log, 18max, 117, 118min, 117, 118open, 81, 82, 135, 138, 139polygon, 31popen, 140randint, 100, 126random, 121, 126
Index 207raw input, 45recursive, 43reload, 142, 196repr,142reversed, 122shuffle, 174sorted, 122sqrt,18, 53str,18sum, 118tuple, 115type, 150zip, 118function argument, 21function call, 17, 26function composition, 54function definition, 19, 20, 25function frame, 23, 26, 43, 109function object, 20, 26function parameter, 21function syntax, 160function typemodifier, 155pure, 154function, fruitful,24function, math, 18function, reasons for,25function, trigonometric, 18function, tuple asreturn value, 117function, void, 24functional programming style, 155, 158gamma function, 58gather, 117, 123GCD (greatestcommon divisor),61generalization, 32, 36, 83, 157geometry manager, 188, 192get method, 105getattr function, 166getcwd function, 137global statement, 110global variable, 110, 113update, 110GNU Free Documentation License, v, vigraphical user interface, 181greatest common divisor (GCD),61grid, 27guardian pattern, 59, 60, 77GUI, 181, 191Gui module, 181gunzip (Unix command), 141Hand class,174hanging, 197HAS-A relationship, 176, 178hasattr function, 150, 166hash function, 108, 113hashable, 108, 113, 120hashtable, 104, 113header, 19, 25, 196Hello, World, 6help utility,8hexadecimal, 146high-level language, 1, 7histogram, 105, 113random choice, 126, 129word frequencies, 127Holmes, Sherlock, 4homophone, 114HTMLParser module, 193hyperlink, 193hypotenuse, 54identical, 100identity, 96if statement, 40Image module, 192image viewer, 192IMDb (Internet Movie Database), 144immutability, 74, 79, 97, 108, 115, 122implementation, 105, 113, 132import statement, 26, 29, 142inoperator, 76, 90, 104increment, 64, 68, 155, 161incremental development, 60, 195indentation, 19, 160, 196index, 71, 77, 79, 89, 99, 103, 198looping with, 84, 91negative, 72slice, 73, 92startingat zero, 71, 90IndexError, 72, 78, 90, 199infinite loop, 65, 69, 181, 197infinite recursion, 44, 47, 58, 197, 198inheritance, 174, 177init method, 162, 166, 170, 172, 174initializationvariable, 68initialization (beforeupdate), 64instance, 29, 36, 146, 151
- Page 1:
Think PythonHowto Think LikeaComput
- Page 4 and 5:
Copyright©2008 AllenDowney.Printin
- Page 6 and 7:
viChapter 0. PrefaceIn 2003 I start
- Page 8 and 9:
viiiChapter 0. Preface• Keith Ver
- Page 10 and 11:
xChapter 0. Preface• Patryk Wolow
- Page 12 and 13:
xiiContents3 Functions 173.1 Functi
- Page 14 and 15:
xivContents7 Iteration 637.1 Multip
- Page 16 and 17:
xviContents12 Tuples 11512.1 Tuples
- Page 18 and 19:
xviiiContents17 Classes and methods
- Page 20 and 21:
xxContents
- Page 22:
2 Chapter 1. The way of theprogramp
- Page 25 and 26:
1.4. Formal and natural languages 5
- Page 27 and 28:
1.7. Glossary 7Your job is to be a
- Page 29 and 30:
Chapter 2Variables,expressions ands
- Page 31 and 32:
2.3. Variable names and keywords 11
- Page 33 and 34:
2.6. Expressions 132.6 ExpressionsA
- Page 35 and 36:
2.10. Debugging 152.10 DebuggingAtt
- Page 37 and 38:
Chapter 3Functions3.1 Functioncalls
- Page 39 and 40:
3.4. Composition 19>>> math.sqrt(2)
- Page 41 and 42:
3.7. Flow of execution 21def repeat
- Page 43 and 44:
3.10. Stack diagrams 23Thisfunction
- Page 45 and 46:
3.12. Why functions? 253.12 Whyfunc
- Page 47 and 48:
3.15. Exercises 27Here’s an examp
- Page 49 and 50:
Chapter 4Case study: interfacedesig
- Page 51 and 52:
4.3. Exercises 31The syntax of a fo
- Page 53 and 54:
4.6. Interface design 33This draws
- Page 55 and 56:
4.8. A development plan 354.8 Adeve
- Page 57 and 58:
4.12. Exercises 37Exercise 4.2 Writ
- Page 59 and 60:
Chapter 5Conditionals and recursion
- Page 61 and 62:
5.5. Alternative execution 41if sta
- Page 63 and 64:
5.9. Stack diagrams for recursive f
- Page 65 and 66:
5.11. Keyboard input 455.11 Keyboar
- Page 67 and 68:
5.14. Exercises 47relational operat
- Page 69 and 70:
5.14. Exercises 492. Write a functi
- Page 71 and 72:
Chapter 6Fruitfulfunctions6.1 Retur
- Page 73 and 74:
6.2. Incremental development 53>>>
- Page 75 and 76:
6.5. More recursion 55It is common
- Page 77 and 78:
6.6. Leap of faith 57__main__factor
- Page 79 and 80:
6.9. Debugging 59The first base cas
- Page 81 and 82:
6.11. Exercises 61sum = x + y + zpo
- Page 83 and 84:
Chapter 7Iteration7.1 Multipleassig
- Page 85 and 86:
7.4. break 652. Iftheconditionisfal
- Page 87 and 88:
7.6. Algorithms 672.00001024003>>>
- Page 89 and 90:
7.9. Exercises 69decrement: An upda
- Page 91 and 92:
Chapter 8Strings8.1 Astringis a seq
- Page 93 and 94:
8.4. String slices 73Thefollowingex
- Page 95 and 96:
8.7. Looping and counting 75This is
- Page 97 and 98:
8.10. String comparison 778.10 Stri
- Page 99 and 100:
8.12. Glossary 798.12 Glossaryobjec
- Page 101 and 102:
Chapter 9Case study: word play9.1 R
- Page 103 and 104:
9.3. Search 83Exercise 9.6 Write a
- Page 105 and 106:
9.5. Debugging 85Here is a version
- Page 107 and 108:
9.7. Exercises 87Write a Python pro
- Page 109 and 110:
Chapter 10Lists10.1 Alist isasequen
- Page 111 and 112:
10.3. Traversing alist 9110.3 Trave
- Page 113 and 114:
10.7. Map, filter and reduce 93List
- Page 115 and 116:
10.9. Listsand strings 95>>> t = ['
- Page 117 and 118:
10.12. Listarguments 97The statedia
- Page 119 and 120:
10.14. Glossary 99word = word.strip
- Page 121 and 122:
10.15. Exercises 101Exercise 10.6 W
- Page 123 and 124:
Chapter 11DictionariesAdictionaryis
- Page 125 and 126:
11.1. Dictionary as asetof counters
- Page 127 and 128:
11.4. Dictionaries and lists 107Thi
- Page 129 and 130:
11.5. Memos 109corresponding locati
- Page 131 and 132:
11.7. Long integers 111count = 0def
- Page 133 and 134:
11.10. Exercises 113implementation:
- Page 135 and 136:
Chapter 12Tuples12.1 Tuples areimmu
- Page 137 and 138:
12.3. Tuples as return values 117>>
- Page 139 and 140:
12.6. Dictionaries and tuples 119If
- Page 141 and 142:
12.7. Comparing tuples 12112.7 Comp
- Page 143 and 144:
12.10. Glossary 123>>> t3 = [1, 2,
- Page 145 and 146:
Chapter 13Case study: data structur
- Page 147 and 148:
13.3. Word histogram 12713.3 Word h
- Page 149 and 150:
13.6. Dictionary subtraction 129num
- Page 151 and 152:
13.9. Datastructures 131In this tex
- Page 153 and 154:
13.11. Glossary 133reading: Examine
- Page 155 and 156:
Chapter 14Files14.1 PersistenceMost
- Page 157 and 158:
14.4. Filenames and paths 137>>> '%
- Page 159 and 160:
14.6. Databases 139Python starts by
- Page 161 and 162:
14.9. Writingmodules 141The argumen
- Page 163 and 164:
14.11. Glossary 14314.11 Glossarype
- Page 165 and 166:
Chapter 15Classes and objects15.1 U
- Page 167 and 168:
15.3. Rectangles 147>>> print '(%g,
- Page 169 and 170:
15.6. Copying 149>>> print box.widt
- Page 171 and 172:
15.8. Glossary 151>>> hasattr(p, 'x
- Page 173 and 174:
Chapter 16Classes and functions16.1
- Page 175 and 176: 16.3. Modifiers 155if sum.second >=
- Page 177 and 178: 16.5. Debugging 157But if we have t
- Page 179 and 180: Chapter 17Classes and methods17.1 O
- Page 181 and 182: 17.3. Another example 161Inside the
- Page 183 and 184: 17.6. The str method 163>>> time =
- Page 185 and 186: 17.9. Polymorphism 165Unfortunately
- Page 187 and 188: 17.12. Exercises 167object-oriented
- Page 189 and 190: Chapter 18InheritanceIn this chapte
- Page 191 and 192: 18.3. Comparing cards 171Thefirstel
- Page 193 and 194: 18.6. Add, remove, shuffle and sort
- Page 195 and 196: 18.8. Classdiagrams 175So when you
- Page 197 and 198: 18.10. Glossary 177might get the on
- Page 199 and 200: 18.11. Exercises 1796. Print a tabl
- Page 201 and 202: Chapter 19Case study: Tkinter19.1 G
- Page 203 and 204: 19.3. Canvas widgets 183Exercise 19
- Page 205 and 206: 19.6. Packing widgets 185>>> text.i
- Page 207 and 208: 19.6. Packing widgets 187self.bu(te
- Page 209 and 210: 19.8. Binding 18919.8 BindingAbindi
- Page 211 and 212: 19.9. Debugging 19119.9 DebuggingOn
- Page 213 and 214: 19.11. Exercises 193image = PIL.ope
- Page 215 and 216: Appendix ADebuggingDifferentkindsof
- Page 217 and 218: A.2. Runtime errors 197A.2 Runtime
- Page 219 and 220: A.3. Semantic errors 199• You are
- Page 221 and 222: A.3. Semantic errors 201The explici
- Page 223 and 224: Indexabecedarian, 72, 83abs functio
- Page 225: Index 205experimental, 4superstitio
- Page 229 and 230: Index 209map pattern, 93, 100map to
- Page 231 and 232: Index 211poker, 169, 178polygon fun
- Page 233 and 234: Index 213knowing the answer, 53leap