Python Workout....1
Praise for the First Edition....3
brief contents....10
contents....11
preface....15
acknowledgments....16
about this book....18
Who should read this book....19
How this book is organized: A road map....19
About the code....20
Software/hardware requirements....21
liveBook discussion forum....21
about the author....22
about the cover illustration....23
1 Improving your Python with practice....25
The role of exercises....26
Scope and goal....27
What’s in the book?....27
What do you need?....28
What about AI?....28
2 Numeric types....31
Useful references....32
Exercise 1 n Number guessing game....32
Working it out....33
Solution....35
Beyond the exercise....36
Exercise 2 n Summing numbers....38
Working it out....38
Solution....39
Beyond the exercise....39
Exercise 3 n Run timing....40
Working it out....41
Solution....42
Beyond the exercise....42
Exercise 4 n Hexadecimal output....43
Working it out....44
Solution....44
Beyond the exercise....45
Summary....45
3 Strings....46
Useful references....47
Exercise 5 n Pig Latin....47
Working it out....48
Solution....49
Beyond the exercise....49
Exercise 6 n Pig Latin sentence....51
Working it out....51
Solution....52
Beyond the exercise....53
Exercise 7 n Ubbi Dubbi....53
Working it out....54
Solution....54
Beyond the exercise....54
Exercise 8 n Sorting a string....55
Working it out....55
Solution....56
Beyond the exercise....56
Summary....57
4 Lists and tuples....58
Useful references....59
Exercise 9 n First-last....60
Working it out....60
Solution....64
Beyond the exercise....64
Exercise 10 n Summing anything....66
Working it out....67
Solution....68
Beyond the exercise....69
Exercise 11 n Alphabetizing countries....69
Working it out....70
Solution....72
Beyond the exercise....72
Exercise 12 n Word with most repeated letters....75
Working it out....76
Solution....77
Beyond the exercise....78
Exercise 13 n Printing tuple records....78
Working it out....79
Solution....79
Beyond the exercise....80
Summary....80
5 Dictionaries and sets....81
Hashing and dicts....82
Sets....83
Useful references....84
Exercise 14 n Restaurant....84
Working it out....85
Solution....86
Beyond the exercise....86
Exercise 15 n Rainfall....87
Working it out....87
Solution....91
Beyond the exercise....91
Exercise 16 n Dictdiff....91
Working it out....92
Solution....95
Beyond the exercise....95
Exercise 17 n How many different numbers?....96
Working it out....96
Solution....97
Beyond the exercise....97
Summary....97
6 Files....99
Useful references....100
Exercise 18 n Final line....100
Working it out....101
Solution....104
Beyond the exercise....105
Exercise 19 n /etc/passwd to dict....106
Working it out....107
Solution....107
Beyond the exercise....108
Exercise 20 n Word count....109
Working it out....110
Solution....112
Beyond the exercise....112
Exercise 21 n Longest word per file....112
Working it out....113
Solution....114
Beyond the exercise....114
Exercise 22 n Reading and writing CSV....116
Working it out....118
Solution....119
Beyond the exercise....119
Exercise 23 n JSON....119
Working it out....120
Solution....122
Beyond the exercise....123
Exercise 24 n Reverse lines....123
Working it out....123
Solution....124
Beyond the exercise....124
Summary....125
7 Functions....126
Useful references....127
Exercise 25 n XML generator....129
Working it out....129
Solution....131
Beyond the exercise....131
Exercise 26 n Prefix notation calculator....135
Working it out....136
Solution....138
Beyond the exercise....138
Exercise 27 n Password generator....139
Working it out....140
Solution....141
Beyond the exercise....142
Summary....142
8 Functional programming with comprehensions....143
Useful references....144
Exercise 28 n Join numbers....145
Working it out....146
Solution....149
Beyond the exercise....149
Exercise 29 n Add numbers....152
Working it out....152
Solution....154
Beyond the exercise....154
Exercise 30 n Flatten a list....154
Working it out....155
Solution....155
Beyond the exercise....155
Exercise 31 n Pig Latin translation of a file....156
Working it out....156
Solution....157
Beyond the exercise....158
Exercise 32 n Flip a dict....158
Working it out....158
Solution....159
Beyond the exercise....159
Exercise 33 n Transform values....160
Working it out....160
Solution....161
Beyond the exercise....161
Exercise 34 n (Almost) supervocalic words....162
Working it out....162
Solution....164
Beyond the exercise....164
Exercise 35a n Gematria, part 1....164
Working it out....165
Solution....165
Beyond the exercise....165
Exercise 35b n Gematria, part 2....166
Working it out....167
Solution....167
Beyond the exercise....168
Summary....168
9 Modules and packages....169
Useful references....170
Exercise 36 n Sales tax....173
Working it out....174
Solution....175
Beyond the exercise....176
Exercise 37 n Menu....177
Working it out....178
Solution....179
Beyond the exercise....179
Summary....181
10 Objects....182
What are objects, anyway?....182
Why use objects?....183
Practicing with objects....185
Useful references....185
Exercise 38 n Ice cream scoop....186
Working it out....187
Solution....188
Beyond the exercise....188
Exercise 39 n Ice cream bowl....191
Working it out....193
Solution....194
Beyond the exercise....195
Exercise 40 n Bowl limits....198
Working it out....198
Solution....200
Beyond the exercise....201
Exercise 41 n A bigger bowl....203
Working it out....203
Solution....204
Beyond the exercise....205
Exercise 42 n FlexibleDict....205
Working it out....206
Solution....207
Beyond the exercise....207
Exercise 43 n Animals....208
Working it out....208
Solution....210
Beyond the exercise....211
Exercise 44 n Cages....211
Working it out....212
Solution....213
Beyond the exercise....214
Exercise 45 n Zoo....214
Working it out....215
Solution....216
Beyond the exercise....217
Summary....217
11 Iterators and generators....218
What is a for loop?....218
Python’s iteration protocol....219
Making your class iterable....219
Other iterators....221
Useful references....221
Exercise 46 n MyEnumerate....223
Working it out....223
Solution....224
Beyond the exercise....225
Exercise 47 n Circle....225
Working it out....225
Solution....226
Beyond the exercise....227
Exercise 48 n All lines, all files....227
Working it out....228
Solution....229
Beyond the exercise....229
Exercise 49 n Elapsed since....230
Working it out....230
Solution....231
Beyond the exercise....231
Exercise 50 n MyChain....232
Working it out....232
Solution....233
Beyond the exercise....233
Summary....233
Conclusion....234
12 Where to from here?....235
What next?....236
Keep in touch....236
index....237
Symbols....237
A....237
B....237
C....237
D....237
E....237
F....237
G....237
H....238
I....238
J....238
K....238
L....238
M....238
N....238
O....238
P....238
R....238
S....238
T....239
V....239
W....239
Y....239
Python Workout - back....241
The only way to master a skill is practice, practice, practice. That’s exactly what you’ll do in this book! In this instantly-valuable book you’ll work through 200 carefully selected exercises that flex your programming muscles.
You’ll practice core Python language features and techniques that reinforce the programming patterns you’ll use every day and build the skills you need to troubleshoot your programs.
Become a champion Python programmer by making its most powerful features second nature! In this unique book, internationally-recognized Python trainer Reuven Lerner builds your mental muscle by guiding you through 200 targeted exercises.
Python Workout, Second Edition is a hands-on training program designed to boost your productivity and code quality. You’ll practice everything from basics like strings and dictionaries to techniques for writing elegant, data-agnostic code with dynamic typing, generic functions, and comprehensions. Each exercise includes a detailed solution along with additional challenges that help you push your skills even farther.
For beginning to intermediate Python programmers.