1 Introduction 1
2 A Case for Puzzle-based Learning 5
2.1 Overcome the Knowledge Gap . . . . . . . 6
2.2 Embrace the Eureka Moment . . . . . . . 7
2.3 Divide and Conquer . . . . . . . . . . . . 8
2.4 Improve From Immediate Feedback . . . . 9
2.5 Measure Your Skills . . . . . . . . . . . . . 10
2.6 Individualized Learning . . . . . . . . . . . 12
2.7 Small is Beautiful . . . . . . . . . . . . . . 12
2.8 Active Beats Passive Learning . . . . . . . 14
2.9 Make Code a First-class Citizen . . . . . . 16
2.10 What You See is All There is . . . . . . . 18
3 The Elo Rating for Python 21
3.1 How to Use This Book . . . . . . . . . . . 21
3.2 How to Test and Train Your Skills? . . . . 23
3.3 What Can This Book Do For You? . . . . 27
4 A Quick Overview of the Python Language 31
4.1 Keywords . . . . . . . . . . . . . . . . . . 32
4.2 Basic Data Types . . . . . . . . . . . . . . 35
4.3 Complex Data Types . . . . . . . . . . . . 38
4.4 Classes . . . . . . . . . . . . . . . . . . . . 43
4.5 Functions and Tricks . . . . . . . . . . . . 46
5 Puzzles: Basic to Scholar 51
5.1 Printing values . . . . . . . . . . . . . . . 51
5.2 Basics of variables . . . . . . . . . . . . . . 53
5.3 Getting started with strings . . . . . . . . 54
5.4 Types of variables I . . . . . . . . . . . . . 55
5.5 Types of Variables II . . . . . . . . . . . . 56
5.6 Minimum . . . . . . . . . . . . . . . . . . 57
5.7 String Concatenation . . . . . . . . . . . . 58
5.8 Line Breaks I . . . . . . . . . . . . . . . . 59
5.9 Line Breaks II . . . . . . . . . . . . . . . . 60
5.10 List Length . . . . . . . . . . . . . . . . . 61
5.11 Comparison Operators I . . . . . . . . . . 62
5.12 Comparison Operators II . . . . . . . . . . 63
5.13 Multiple Initializations . . . . . . . . . . . 64
6 Puzzles: Scholar to Intermediate 67
6.1 Maximum . . . . . . . . . . . . . . . . . . 67
6.2 Memory addresses . . . . . . . . . . . . . . 68
6.3 Swapping Values . . . . . . . . . . . . . . 69
6.4 The Boolean Operator AND . . . . . . . . 70
6.5 The Boolean Operator OR . . . . . . . . . 72
6.6 Boolean Operators . . . . . . . . . . . . . 74
6.7 Arithmetic Expressions . . . . . . . . . . . 76
6.8 Integer Division and Modulo . . . . . . . . 78
6.9 Building Strings . . . . . . . . . . . . . . . 80
6.10 The len() Function . . . . . . . . . . . . 81
6.11 String Indices . . . . . . . . . . . . . . . . 82
6.12 The upper() Function . . . . . . . . . . . . 83
6.13 The lower() Function . . . . . . . . . . . . 84
6.14 Somebody Is Shouting . . . . . . . . . . . 85
6.15 Counting Characters . . . . . . . . . . . . 86
6.16 String Lengths . . . . . . . . . . . . . . . 87
6.17 Finding Characters in Strings . . . . . . . 88
6.18 Not Finding Characters in Strings . . . . . 89
6.19 Counting Letters . . . . . . . . . . . . . . 90
6.20 Min() and Max() of a String . . . . . . . . 91
6.21 Reversed Strings . . . . . . . . . . . . . . 92
6.22 String Equality . . . . . . . . . . . . . . . 93
6.23 Slicing I . . . . . . . . . . . . . . . . . . . 95
6.24 Slicing II . . . . . . . . . . . . . . . . . . . 96
6.25 Slicing III . . . . . . . . . . . . . . . . . . 97
6.26 Slicing IV . . . . . . . . . . . . . . . . . . 98
6.27 Slicing V . . . . . . . . . . . . . . . . . . . 99
6.28 Memory Addresses and Slicing . . . . . . . 100
6.29 Accessing List Items I . . . . . . . . . . . 101
6.30 Accessing List Items II . . . . . . . . . . . 103
6.31 List as Stack . . . . . . . . . . . . . . . . . 104
6.32 More String Operations . . . . . . . . . . 105
6.33 Checking for Substrings . . . . . . . . . . 106
6.34 Stripping String Boundaries . . . . . . . . 107
6.35 Strings: Stripping vs. Replacement . . . . 108
6.36 Glueing Strings Together . . . . . . . . . . 109
6.37 The Copy Operation . . . . . . . . . . . . 110
6.38 Growing List Contents I . . . . . . . . . . 111
6.39 Growing List Contents II . . . . . . . . . . 112
6.40 List Operations I . . . . . . . . . . . . . . 114
6.41 List Operations II . . . . . . . . . . . . . . 115
6.42 List Operations III . . . . . . . . . . . . . 116
6.43 List Operations IV . . . . . . . . . . . . . 117
6.44 List Operations V . . . . . . . . . . . . . . 117
6.45 List Operations VI . . . . . . . . . . . . . 118
6.46 List Operations VII . . . . . . . . . . . . . 119
6.47 List Operations VIII . . . . . . . . . . . . 120
6.48 List Operations IX . . . . . . . . . . . . . 121
6.49 List Operations X . . . . . . . . . . . . . . 122
6.50 Lists and the Range Function I . . . . . . 123
6.51 Lists and the Range Function II . . . . . . 124
6.52 Lists and the Range Function III . . . . . 125
6.53 Python’s Multiple Assignment I . . . . . . 126
6.54 Slice Assignments . . . . . . . . . . . . . . 127
6.55 Strings and Lists II . . . . . . . . . . . . . 128
6.56 String Comparisons . . . . . . . . . . . . . 129
6.57 From Booleans to Strings . . . . . . . . . . 130
6.58 Boolean Trickery I . . . . . . . . . . . . . 131
6.59 Boolean Trickery II . . . . . . . . . . . . . 132
6.60 Boolean Trickery III . . . . . . . . . . . . 133
6.61 Looping over Ranges . . . . . . . . . . . . 134
6.62 Reversed Loops . . . . . . . . . . . . . . . 135
6.63 Boolean Trickery IV . . . . . . . . . . . . 136
6.64 Lists and Memory Addresses . . . . . . . . 137
6.65 List Objects . . . . . . . . . . . . . . . . . 138
6.66 Boolean Tricks . . . . . . . . . . . . . . . 139
6.67 Complex Numbers . . . . . . . . . . . . . 140
6.68 Tuples . . . . . . . . . . . . . . . . . . . . 141
6.69 Multiple Assignments . . . . . . . . . . . . 142
6.70 Boolean Integer Conversion . . . . . . . . 143
6.71 The any() Function . . . . . . . . . . . . 144
6.72 The sum() Function . . . . . . . . . . . . 146
6.73 Accessing Complex Numbers . . . . . . . . 148
6.74 Tuple Confusion . . . . . . . . . . . . . . . 149
6.75 Understanding While ... Else (1/3) . . . . 150
6.76 Understanding While ... Else (2/3) . . . . 151
6.77 Understanding While ... Else (3/3) . . . . 153
6.78 Basic Arithmetic . . . . . . . . . . . . . . 155
6.79 Dictionary . . . . . . . . . . . . . . . . . . 156
6.80 Dictionary of Dictionaries . . . . . . . . . 158
6.81 Reverse Dictionary Index . . . . . . . . . . 160
6.82 Default Arguments . . . . . . . . . . . . . 162
7 Puzzles: Intermediate to Professional 165
7.1 Building Strings II . . . . . . . . . . . . . 165
7.2 String: Slicing and Indexing . . . . . . . . 167
7.3 Built-in Python Operations . . . . . . . . 168
7.4 Strings and Lists I . . . . . . . . . . . . . 169
7.5 Formatting Printouts . . . . . . . . . . . . 170
7.6 Floating Point Comparisons . . . . . . . . 172
7.7 Python’s Multiple Assignment II . . . . . 174
7.8 The Not-So-Obvious Case . . . . . . . . . 175
7.9 Rounding Values . . . . . . . . . . . . . . 176
7.10 Initializing Integers . . . . . . . . . . . . . 177
7.11 Basic Typing . . . . . . . . . . . . . . . . 178
7.12 Short Circuiting . . . . . . . . . . . . . . . 179
7.13 While Arithmetic . . . . . . . . . . . . . . 182
7.14 The Lambda Function . . . . . . . . . . . 183
7.15 Zip . . . . . . . . . . . . . . . . . . . . . . 184
7.16 Basic Filtering . . . . . . . . . . . . . . . . 186
7.17 List Comprehension . . . . . . . . . . . . . 188
7.18 Encryption by Obfuscation . . . . . . . . . 190
7.19 String Dictionary . . . . . . . . . . . . . . 193
7.20 Lambda Function . . . . . . . . . . . . . . 194
7.21 Dictionary of Dictionaries . . . . . . . . . 196
7.22 Sorting Dictionary Keys . . . . . . . . . . 198
7.23 Pythonic Loop Iteration . . . . . . . . . . 200
7.24 Filtering with List Comprehension . . . . . 202
7.25 Aggregating with List Comprehension . . . 204
7.26 Maximum of Tuples . . . . . . . . . . . . . 206
7.27 The Key Argument . . . . . . . . . . . . . 208
7.28 Puzzle 123 . . . . . . . . . . . . . . . . . . 210
7.29 Set Operations (1/2) . . . . . . . . . . . . 211
7.30 Set Operations (2/2) . . . . . . . . . . . . 212
7.31 Recursive Algorithm . . . . . . . . . . . . 214
7.32 Fibonacci . . . . . . . . . . . . . . . . . . 218
8 Final Remarks 221
Your skill level . . . . . . . . . . . . . . . 221
Where to go from here? . . . . . . . . . . 222