If you want to learn how to program, working with Python is an excellent way to start. This hands-on guide takes you through the language one step at a time, beginning with basic programming concepts before moving on to functions, recursion, data structures, and object-oriented design. Through exercises in each chapter, you’ll try out programming concepts as you learn them. Preface 5 The way of the program 23 The Python programming language 23 What is a program? 25 What is debugging? 25 Formal and natural languages 27 The first program 28 Debugging 29 Glossary 29 Exercises 31 Variables, expressions and statements 33 Values and types 33 Variables 34 Variable names and keywords 35 Operators and operands 35 Expressions and statements 36 Interactive mode and script mode 36 Order of operations 37 String operations 38 Comments 38 Debugging 39 Glossary 39 Exercises 40 Functions 41 Function calls 41 Type conversion functions 41 Math functions 42 Composition 43 Adding new functions 43 Definitions and uses 44 Flow of execution 45 Parameters and arguments 45 Variables and parameters are local 46 Stack diagrams 47 Fruitful functions and void functions 48 Why functions? 48 Importing with from 49 Debugging 49 Glossary 50 Exercises 51 Case study: interface design 53 TurtleWorld 53 Simple repetition 54 Exercises 55 Encapsulation 56 Generalization 56 Interface design 57 Refactoring 58 A development plan 59 docstring 59 Debugging 60 Glossary 60 Exercises 61 Conditionals and recursion 63 Modulus operator 63 Boolean expressions 63 Logical operators 64 Conditional execution 64 Alternative execution 65 Chained conditionals 65 Nested conditionals 65 Recursion 66 Stack diagrams for recursive functions 67 Infinite recursion 68 Keyboard input 68 Debugging 69 Glossary 70 Exercises 71 Fruitful functions 73 Return values 73 Incremental development 74 Composition 76 Boolean functions 76 More recursion 77 Leap of faith 79 One more example 79 Checking types 80 Debugging 81 Glossary 82 Exercises 82 Iteration 85 Multiple assignment 85 Updating variables 86 The while statement 86 break 87 Square roots 88 Algorithms 89 Debugging 90 Glossary 90 Exercises 91 Strings 93 A string is a sequence 93 len 93 Traversal with a for loop 94 String slices 95 Strings are immutable 96 Searching 96 Looping and counting 97 String methods 97 The in operator 98 String comparison 98 Debugging 99 Glossary 100 Exercises 101 Case study: word play 103 Reading word lists 103 Exercises 104 Search 104 Looping with indices 105 Debugging 107 Glossary 107 Exercises 108 Lists 109 A list is a sequence 109 Lists are mutable 109 Traversing a list 111 List operations 111 List slices 111 List methods 112 Map, filter and reduce 113 Deleting elements 114 Lists and strings 115 Objects and values 115 Aliasing 116 List arguments 117 Debugging 118 Glossary 119 Exercises 120 Dictionaries 123 Dictionary as a set of counters 124 Looping and dictionaries 125 Reverse lookup 126 Dictionaries and lists 127 Memos 128 Global variables 130 Long integers 131 Debugging 131 Glossary 132 Exercises 133 Tuples 135 Tuples are immutable 135 Tuple assignment 136 Tuples as return values 137 Variable-length argument tuples 137 Lists and tuples 138 Dictionaries and tuples 139 Comparing tuples 140 Sequences of sequences 141 Debugging 142 Glossary 143 Exercises 143 Case study: data structure selection 145 Word frequency analysis 145 Random numbers 146 Word histogram 147 Most common words 148 Optional parameters 148 Dictionary subtraction 149 Random words 149 Markov analysis 150 Data structures 151 Debugging 153 Glossary 154 Exercises 154 Files 155 Persistence 155 Reading and writing 155 Format operator 156 Filenames and paths 157 Catching exceptions 158 Databases 159 Pickling 159 Pipes 160 Writing modules 161 Debugging 162 Glossary 163 Exercises 163 Classes and objects 165 User-defined types 165 Attributes 166 Rectangles 167 Instances as return values 168 Objects are mutable 168 Copying 169 Debugging 170 Glossary 171 Exercises 171 Classes and functions 173 Time 173 Pure functions 173 Modifiers 175 Prototyping versus planning 176 Debugging 177 Glossary 177 Exercises 178 Classes and methods 179 Object-oriented features 179 Printing objects 180 Another example 181 A more complicated example 182 The init method 182 The __str__ method 183 Operator overloading 183 Type-based dispatch 184 Polymorphism 185 Debugging 186 Interface and implementation 186 Glossary 187 Exercises 187 Inheritance 189 Card objects 189 Class attributes 190 Comparing cards 191 Decks 192 Printing the deck 193 Add, remove, shuffle and sort 193 Inheritance 194 Class diagrams 195 Debugging 196 Data encapsulation 197 Glossary 198 Exercises 199 Case study: Tkinter 201 GUI 201 Buttons and callbacks 202 Canvas widgets 203 Coordinate sequences 204 More widgets 204 Packing widgets 205 Menus and Callables 207 Binding 208 Debugging 210 Glossary 211 Exercises 212 Debugging 215 Syntax errors 215 Runtime errors 217 Semantic errors 220 Analysis of Algorithms 223 Order of growth 224 Analysis of basic Python operations 226 Analysis of search algorithms 227 Hashtables 228 Lumpy 233 State diagram 233 Stack diagram 234 Object diagrams 235 Function and class objects 237 Class Diagrams 238