Foreword....12
Pydon’t disrespect the Zen of Python....13
Zen of Python....14
References....15
Zip up....16
Introduction....16
How zip works....17
Zip is lazy....18
Three is a crowd....19
Mismatched lengths....19
Create a dictionary with zip....20
Examples in code....20
Conclusion....23
References....24
Enumerate me....25
Introduction....25
How enumerate works....26
Optional start argument....27
Unpacking when iterating....27
Examples in code....29
Conclusion....34
References....35
Chaining comparison operators....36
Introduction....37
Chaining of comparison operators....37
Pitfalls....38
Ugly chains....39
Examples in code....40
Conclusion....41
References....41
Boolean short-circuiting....42
Introduction....42
Return values of the and and or operators....43
Short-circuiting....44
Short-circuiting in plain English....46
all and any....47
Short-circuiting in chained comparisons....47
Examples in code....47
Conclusion....56
References....56
set and frozenset....58
Introduction....58
(Mathematical) sets....59
(Common) Operations on sets....60
Differences between set and frozenset....63
What are sets used for?....65
Examples in code....66
Conclusion....70
References....70
List comprehensions 101....71
Introduction....71
What is a list comprehension?....72
A loop that builds a list....72
Filtering data in a list comprehension....74
List comprehensions from first principles....76
Full anatomy of a list comprehension....77
Advantages of list comprehensions....80
Examples in code....81
Conclusion....85
References....85
Sequence indexing....86
Introduction....86
Sequence indexing....87
Maximum legal index and index errors....88
Negative indices....88
Indexing idioms....90
To index or not to index?....90
Best practices in code....91
Conclusion....94
Idiomatic sequence slicing....96
Introduction....96
Slicing syntax....97
What to slice?....98
Slicing from the beginning....99
Slicing until the end....100
Slicing with negative indices....100
Slicing and range....101
Idiomatic slicing patterns....102
Empty slices....103
More empty slices....104
Examples in code....104
Conclusion....107
References....108
String formatting comparison....109
Introduction....109
String formatting rationale....110
Three string formatting methods....111
Value conversion....112
Alignment....113
Named placeholders....114
Accessing nested data structures....114
Parametrised formatting....115
Custom formatting....116
Examples in code....117
Conclusion....118
References....119
Truthy, Falsy, and bool....120
“Truthy” and “Falsy”....122
The __bool__ dunder method....123
Remarks....124
Examples in code....125
Conclusion....128
References....128
Assignment expressions and the walrus operator :=....129
Walrus operator and assignment expressions....129
Examples in code....130
Conclusion....133
References....133
String translate and maketrans methods....134
Introduction....134
str.translate....135
Non-equivalence to str.replace....137
Generic translation tables....138
str.maketrans....139
Examples in code....141
Conclusion....145
References....146
Conditional expressions....147
Introduction....147
What is a conditional expression?....148
Rationale....150
Short-circuiting....151
Conditional expressions and if statements....153
Precedence....156
Conditional expressions that evaluate to Booleans....157
Examples in code....158
Conclusion....159
References....160
Dunder methods....161
Introduction....161
What are dunder methods?....162
List of dunder methods and their interactions....164
Exploring a dunder method....167
Conclusion....169
References....170
Overloading arithmetic operators with dunder methods....171
Introduction....173
The example we will be using....174
Your first arithmetic dunder method....174
The dunder methods for the unary arithmetic operations....176
The dunder methods for binary arithmetic operations....179
Addition and the dunder method __add__....179
Adding validation to your dunder methods....180
Using NotImplemented to flag operations you don’t support....181
Extending your dunder method to more types....183
Reflected dunder methods....185
Implementing reflected dunder methods....185
Augmented arithmetic assignment....190
Full implementation of all arithmetic dunder methods....191
Exercises....216
Conclusion....216
References....217
str and repr....218
str and repr....220
The __str__ and __repr__ dunder methods....220
Examples in code....221
Conclusion....223
References....223
Deep unpacking....224
Introduction....224
Assignments....225
Examples in code....227
Conclusion....229
References....229
Unpacking with starred assignments....231
Starred Assignment....232
Examples in code....233
References....235
Structural pattern matching tutorial....236
Introduction....237
Structural pattern matching Python could already do....237
Your first match statement....237
Pattern matching the basic structure....238
Matching the structure of objects....240
__match_args__....241
Wildcards....242
Naming sub-patterns....244
Traversing recursive structures....245
Conclusion....247
References....247
Structural pattern matching anti-patterns....249
Introduction....250
There should be only one obvious way to do it....250
A short and sweet if statement....250
Be smart(er)....251
Basic mappings....252
Conclusion....257
References....258
Mastering sequence slicing....259
Introduction....259
Slicing step....260
Sequence copying....267
Manipulating mutable sequences....267
More idiomatic slicing....270
Conclusion....273
References....274
Inner workings of sequence slicing....275
Introduction....276
The slice class....276
Getting items from sequences....278
Setting items, deleting items, and container emulation....279
Comma-separated indices and slices....279
Examples in code....280
Conclusion....284
References....284
Properties....286
Introduction....286
What is a property?....287
Common property use cases....289
Properties with setters....293
Property deleters....295
The property descriptor....297
Conclusion....299
References....299
Describing Descriptors....300
Introduction....301
The problem that descriptors solve....301
Your first descriptor....302
Accessing the name of the original attribute....306
Descriptor exercises....309
Descriptor protocol....311
The dunder method __set__....311
Accessing the descriptor objects....315
Built-in descriptors....317
Methods and descriptors....326
Data and non-data descriptors....329
Complete descriptor example: attribute validation....333
Descriptors in the standard library....334
Conclusion....335
References....335
Boost your productivity with the REPL....337
Introduction....337
REPL....338
Just fire up the REPL....338
REPL mechanics....338
The last result....342
Getting help from within the REPL....343
Tips for quick hacks....344
Other tools....346
Conclusion....347
References....347
The power of reduce....348
Introduction....348
How reduce works....349
The rabbit hole of the built-in reductions....350
Why bother?....351
Far-fetched reductions....351
The identity element…....353
Why some people dislike reduce....355
Examples in code....355
Conclusion....357
References....357
Usages of underscore....359
Introduction....359
Recovering last result in the session....360
Prefixes and suffixes for variable names....361
Underscore as a sink....367
Matching everything in the new match statement....369
String localisation....370
Improve number readability....371
Conclusion....372
References....372
name dunder attribute....374
Introduction....374
What is __name__?....375
The module attribute __name__....375
__name__ as an object type attribute....377
Examples in code....379
Conclusion....383
References....383
Pass-by-value, reference, and assignment....384
Introduction....386
Is Python pass-by-value?....386
Is Python pass-by-reference?....387
Python object model....388
Python is pass-by-assignment....392
Making copies....393
Examples in code....396
Conclusion....400
References....400
Watch out for recursion....401
Introduction....402
Watch out for recursion....402
Examples in code....405
Conclusion....411
References....412
EAFP and LBYL coding styles....413
EAFP and LBYL....413
EAFP instead of LBYL?....414
Conclusion....418
References....418
Bite-sized refactoring....419
Introduction....419
Refactoring....420
What to refactor?....421
Case study....421
Conclusion....428
References....429
Naming matters....430
Introduction....430
Naming conventions....431
PEP 8 recommendations....431
Standard names....433
Verbosity....434
Picking a name....437
Context is key....439
Practical example....439
Conclusion....441
References....442
Does elegance matter?....443
Introduction....443
Beware, opinions ahead....444
Elegance is not a dispensable luxury....444
Don’t write that, that’s unreadable....444
The right tool for the job....445
Optimise for rewrite....446
Conclusion....446
References....447
Code style matters....448
Introduction....449
Code style....449
Tools for your tool belt....450
Conclusion....453
References....453
Why mastering Python is impossible, and why that’s ok....455
Introduction....456
“to master”, verb....456
How to get as close to mastering Python as possible....457
Anecdote with string methods....461
Conclusion....462
References....462
Closing thoughts....463
Python has so many libraries that people often forget to take their time to learn about all the really interesting and useful features that Python offers.The Pydon'ts teach you these core features of Python, with plenty of code examples to show you how these features are used in real code in the real world.Want to master Python? Start here?