Contents xxi 4.7 Nested conditionals........·.········· 41 4.8 The return statement····· 4 42 4.9 Recursion 42 4.10 Stack diagrams for recursive functions.····.···.· 4 44 4.11 Infinite recursion 45 4.12 Keyboard input.. 45 4.13 Glossary···· 46 5 Fruitful functions 49 5.1 Return values..... 49 5.2 Program development 。4 50 5.3 Composition.·... 53 5.4 Boolean functions... 54 5.5 More recursion..·. 4 55 5.6 Leap of faith 57 5.7 One more example 58 5.8 Checking types 58 5.9 Glossary 60 6 Iteration 61 6.1 Multiple assignment.. 61 6.2 The while statement... 62 6.3 Tables......... 64 6.4 Two-dimensional tables..... 66 6.5 Encapsulation and generalization 67 6.6 More encapsulation ..... 68 6.7 Local variables 4 4 69 6.8 More generalization.. 70 6.9 Functions 71 6.10 Glossary····· 72
Contents xxi 4.7 Nested conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . 41 4.8 The return statement . . . . . . . . . . . . . . . . . . . . . . . . 42 4.9 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 4.10 Stack diagrams for recursive functions . . . . . . . . . . . . . . . 44 4.11 Infinite recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 4.12 Keyboard input . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 4.13 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 5 Fruitful functions 49 5.1 Return values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 5.2 Program development . . . . . . . . . . . . . . . . . . . . . . . . 50 5.3 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 5.4 Boolean functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 5.5 More recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 5.6 Leap of faith . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 5.7 One more example . . . . . . . . . . . . . . . . . . . . . . . . . . 58 5.8 Checking types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 5.9 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 6 Iteration 61 6.1 Multiple assignment . . . . . . . . . . . . . . . . . . . . . . . . . 61 6.2 The while statement . . . . . . . . . . . . . . . . . . . . . . . . . 62 6.3 Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 6.4 Two-dimensional tables . . . . . . . . . . . . . . . . . . . . . . . 66 6.5 Encapsulation and generalization . . . . . . . . . . . . . . . . . . 67 6.6 More encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . 68 6.7 Local variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 6.8 More generalization . . . . . . . . . . . . . . . . . . . . . . . . . . 70 6.9 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 6.10 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
xxii Contents 7 Strings 73 7.1 A compound data type.·· 73 7.2 Length 74 7.3 Traversal and the for loop.. 74 7.4 String slices...·.·.· 4 76 7.5 String comparison 76 7.6 Strings are immutable 4 77 7.7 A find function...·. 78 7.8 Looping and counting 78 7.9 The string module 79 7.10 Character classification. 80 7.11 Glossary ..... 81 8 Lists 83 8.1 List values.····· 83 8.2 Accessing elements··· 84 8.3 List length.:····· 85 8.4 List membership 86 8.5 Lists and for loops. 86 8.6 List operations····· 87 8.7 List slices 88 8.8 Lists are mutable 4 88 8.9 List deletion..... 89 8.10 Objects and values 91 8.11 Aliasing·。.· 92 8.12 Cloning lists.. 92 8.13 List parameters . 93 8.14 Nested lists..... 94
xxii Contents 7 Strings 73 7.1 A compound data type . . . . . . . . . . . . . . . . . . . . . . . . 73 7.2 Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 7.3 Traversal and the for loop . . . . . . . . . . . . . . . . . . . . . . 74 7.4 String slices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 7.5 String comparison . . . . . . . . . . . . . . . . . . . . . . . . . . 76 7.6 Strings are immutable . . . . . . . . . . . . . . . . . . . . . . . . 77 7.7 A find function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 7.8 Looping and counting . . . . . . . . . . . . . . . . . . . . . . . . 78 7.9 The string module . . . . . . . . . . . . . . . . . . . . . . . . . 79 7.10 Character classification . . . . . . . . . . . . . . . . . . . . . . . . 80 7.11 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 8 Lists 83 8.1 List values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 8.2 Accessing elements . . . . . . . . . . . . . . . . . . . . . . . . . . 84 8.3 List length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 8.4 List membership . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 8.5 Lists and for loops . . . . . . . . . . . . . . . . . . . . . . . . . . 86 8.6 List operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 8.7 List slices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 8.8 Lists are mutable . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 8.9 List deletion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 8.10 Objects and values . . . . . . . . . . . . . . . . . . . . . . . . . . 91 8.11 Aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 8.12 Cloning lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 8.13 List parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 8.14 Nested lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Contents xxiii 8.15 Matrices...······· 94 8.16 Strings and lists..... 95 8.17 Glossary。········ 96 9 Tuples 97 9.1 Mutability and tuples 97 9.2 Tuple assignment..... 98 9.3 Tuples as return values . 99 9.4 Random numbers.·.··· 99 9.5 List of random numbers 100 9.6 Counting:..······ 4 。 。 101 9.7 Many buckets ... 102 9.8 A single-pass solution.. 104 9.9 Glossary···· 4 105 10 Dictionaries 107 10.1 Dictionary operations... 108 10.2 Dictionary methods.. 。 109 10.3 Aliasing and copying 4 110 10.4 Sparse matrices 110 10.5 Hints.····· 111 10.6 Long integers·· 113 10.7 Counting letters... 113 10.8 Glossary.·:··· 114 11 Files and exceptions 117 11.1 Text files.... 119 11.2 Vriting variables·.. 120 11.3 Directories..... 123
Contents xxiii 8.15 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 8.16 Strings and lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 8.17 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 9 Tuples 97 9.1 Mutability and tuples . . . . . . . . . . . . . . . . . . . . . . . . 97 9.2 Tuple assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 9.3 Tuples as return values . . . . . . . . . . . . . . . . . . . . . . . . 99 9.4 Random numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 9.5 List of random numbers . . . . . . . . . . . . . . . . . . . . . . . 100 9.6 Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 9.7 Many buckets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 9.8 A single-pass solution . . . . . . . . . . . . . . . . . . . . . . . . . 104 9.9 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 10 Dictionaries 107 10.1 Dictionary operations . . . . . . . . . . . . . . . . . . . . . . . . . 108 10.2 Dictionary methods . . . . . . . . . . . . . . . . . . . . . . . . . . 109 10.3 Aliasing and copying . . . . . . . . . . . . . . . . . . . . . . . . . 110 10.4 Sparse matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 10.5 Hints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 10.6 Long integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 10.7 Counting letters . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 10.8 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 11 Files and exceptions 117 11.1 Text files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 11.2 Writing variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 11.3 Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
xxiv Contents 11.4 Pickling··········· 123 11.5 Exceptions,···· 124 11.6 Glossary······ 126 12 Classes and objects 129 12.1 User-defined compound types . 129 12.2 Attributes......... 。。 130 12.3 Instances as arguments... 131 12.4 Sameness....·.··· 131 12.5 Rectangles....·.·· 4 133 12.6 Instances as return values 134 12.7 Objects are mutable 134 12.8 Copying 135 12.9 Glossary···· 137 13 Classes and functions 139 13.1 Time.······· 4 139 13.2 Pure functions... 44 140 13.3 Modifiers 44 141 13.4 Which is better? 142 13.5 Prototype development versus planning 143 13.6 Generalization.... 144 13.7 Algorithms 。。 144 13.8 Glossary .... 145 14 Classes and methods 147 14.1 Object-oriented features.. 147 14.2 printTime.···· 148 14.3 Another example 。。。 149
xxiv Contents 11.4 Pickling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 11.5 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 11.6 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 12 Classes and objects 129 12.1 User-defined compound types . . . . . . . . . . . . . . . . . . . . 129 12.2 Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 12.3 Instances as arguments . . . . . . . . . . . . . . . . . . . . . . . . 131 12.4 Sameness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 12.5 Rectangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 12.6 Instances as return values . . . . . . . . . . . . . . . . . . . . . . 134 12.7 Objects are mutable . . . . . . . . . . . . . . . . . . . . . . . . . 134 12.8 Copying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 12.9 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 13 Classes and functions 139 13.1 Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 13.2 Pure functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 13.3 Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 13.4 Which is better? . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 13.5 Prototype development versus planning . . . . . . . . . . . . . . 143 13.6 Generalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 13.7 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 13.8 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 14 Classes and methods 147 14.1 Object-oriented features . . . . . . . . . . . . . . . . . . . . . . . 147 14.2 printTime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 14.3 Another example . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Contents XXV 14.4 A more complicated example 150 14.5 Optional arguments.···· 151 14.6 The initialization method 152 14.7 Points revisited..... 153 14.8 Operator overloading.... 154 14.9 Polymorphism...·. 155 14.10G1 lossary.·.···· 157 15 Sets of objects 159 15.1 Composition...... 159 15.2 Card objects.......... 159 15.3 Class attributes and the_str__method 161 15.4 Comparing cards 162 15.5 Decks 163 15.6 Printing the deck 163 15.7 Shuffling the deck.···· 165 15.8 Removing and dealing cards 166 15.9 Glossary.········ 167 16 Inheritance 169 16.1 Inheritance... 169 16.2 A hand of cards ... 170 16.3 Dealing cards .... 171 16.4 Printing a Hand... 171 16.5 The CardGame class.. 172 16.6 01dMaidHand class 173 16.7 0ldMaidGame class .. 175 16.8 Glossary······ 179
Contents xxv 14.4 A more complicated example . . . . . . . . . . . . . . . . . . . . 150 14.5 Optional arguments . . . . . . . . . . . . . . . . . . . . . . . . . . 151 14.6 The initialization method . . . . . . . . . . . . . . . . . . . . . . 152 14.7 Points revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 14.8 Operator overloading . . . . . . . . . . . . . . . . . . . . . . . . . 154 14.9 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 14.10 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 15 Sets of objects 159 15.1 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 15.2 Card objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 15.3 Class attributes and the str method . . . . . . . . . . . . . . 161 15.4 Comparing cards . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 15.5 Decks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 15.6 Printing the deck . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 15.7 Shuffling the deck . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 15.8 Removing and dealing cards . . . . . . . . . . . . . . . . . . . . . 166 15.9 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 16 Inheritance 169 16.1 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 16.2 A hand of cards . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 16.3 Dealing cards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 16.4 Printing a Hand . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 16.5 The CardGame class . . . . . . . . . . . . . . . . . . . . . . . . . . 172 16.6 OldMaidHand class . . . . . . . . . . . . . . . . . . . . . . . . . . 173 16.7 OldMaidGame class . . . . . . . . . . . . . . . . . . . . . . . . . . 175 16.8 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179