<p>Preface xiii<br><strong>Chapter 1: Python Basics 1</strong><br>1.1 Running Python 1<br>1.2 Python Programs 2<br>1.3 Primitives, Variables, and Expressions 3<br>1.4 Arithmetic Operators 5<br>1.5 Conditionals and Control Flow 7<br>1.6 Text Strings 9<br>1.7 File Input and Output 12<br>1.8 Lists 13<br>1.9 Tuples 15<br>1.10 Sets 17<br>1.11 Dictionaries 18<br>1.12 Iteration and Looping 21<br>1.13 Functions 22<br>1.14 Exceptions 24<br>1.15 Program Termination 26<br>1.16 Objects and Classes 26<br>1.17 Modules 30<br>1.18 Script Writing 32<br>1.19 Packages 33<br>1.20 Structuring an Application 34<br>1.21 Managing Third-Party Packages 35<br>1.22 Python: It Fits Your Brain 36<br><strong>Chapter 2: Operators, Expressions, and Data Manipulation 37</strong><br>2.1 Literals 37<br>2.2 Expressions and Locations 38<br>2.3 Standard Operators 39<br>2.4 In-Place Assignment 41<br>2.5 Object Comparison 42<br>2.6 Ordered Comparison Operators 42<br>2.7 Boolean Expressions and Truth Values 43<br>2.8 Conditional Expressions 44<br>2.9 Operations Involving Iterables 45<br>2.10 Operations on Sequences 47<br>2.11 Operations on Mutable Sequences 49<br>2.12 Operations on Sets 50<br>2.13 Operations on Mappings 51<br>2.14 List, Set, and Dictionary Comprehensions 52<br>2.15 Generator Expressions 54<br>2.16 The Attribute (.) Operator 56<br>2.17 The Function Call () Operator 56<br>2.18 Order of Evaluation 56<br>2.19 Final Words: The Secret Life of Data 58<br><strong>Chapter 3: Program Structure and Control Flow 59</strong><br>3.1 Program Structure and Execution 59<br>3.2 Conditional Execution 59<br>3.3 Loops and Iteration 60<br>3.4 Exceptions 64<br>3.5 Context Managers and the with Statement 75<br>3.6 Assertions and __debug__ 77<br>3.7 Final Words 78<br><strong>Chapter 4: Objects, Types, and Protocols 79</strong><br>4.1 Essential Concepts 79<br>4.2 Object Identity and Type 80<br>4.3 Reference Counting and Garbage Collection 81<br>4.4 References and Copies 83<br>4.5 Object Representation and Printing 84<br>4.6 First-Class Objects 85<br>4.7 Using None for Optional or Missing Data 87<br>4.8 Object Protocols and Data Abstraction 87<br>4.9 Object Protocol 89<br>4.10 Number Protocol 90<br>4.11 Comparison Protocol 92<br>4.12 Conversion Protocols 94<br>4.13 Container Protocol 95<br>4.14 Iteration Protocol 97<br>4.15 Attribute Protocol 98<br>4.16 Function Protocol 98<br>4.17 Context Manager Protocol 99<br>4.18 Final Words: On Being Pythonic 99<br><strong>Chapter 5: Functions 101</strong><br>5.1 Function Definitions 101<br>5.2 Default Arguments 101<br>5.3 Variadic Arguments 102<br>5.4 Keyword Arguments 103<br>5.5 Variadic Keyword Arguments 104<br>5.6 Functions Accepting All Inputs 104<br>5.7 Positional-Only Arguments 105<br>5.8 Names, Documentation Strings, and Type Hints 106<br>5.9 Function Application and Parameter Passing 107<br>5.10 Return Values 109<br>5.11 Error Handling 110<br>5.12 Scoping Rules 111<br>5.13 Recursion 114<br>5.14 The lambda Expression 114<br>5.15 Higher-Order Functions 115<br>5.16 Argument Passing in Callback Functions 118<br>5.17 Returning Results from Callbacks 121<br>5.18 Decorators 124<br>5.19 Map, Filter, and Reduce 127<br>5.20 Function Introspection, Attributes, and Signatures 129<br>5.21 Environment Inspection 131<br>5.22 Dynamic Code Execution and Creation 133<br>5.23 Asynchronous Functions and await 135<br>5.24 Final Words: Thoughts on Functions and Composition 137<br><strong>Chapter 6: Generators 139</strong><br>6.1 Generators and yield 139<br>6.2 Restartable Generators 142<br>6.3 Generator Delegation 142<br>6.4 Using Generators in Practice 144<br>6.5 Enhanced Generators and yield Expressions 146<br>6.6 Applications of Enhanced Generators 148<br>6.7 Generators and the Bridge to Awaiting 151<br>6.8 Final Words: A Brief History of Generators and Looking Forward 152<br><strong>Chapter 7: Classes and Object-Oriented Programming 153</strong><br>7.1 Objects 153<br>7.2 The class Statement 154<br>7.3 Instances 155<br>7.4 Attribute Access 156<br>7.5 Scoping Rules 158<br>7.6 Operator Overloading and Protocols 159<br>7.7 Inheritance 160<br>7.8 Avoiding Inheritance via Composition 163<br>7.9 Avoiding Inheritance via Functions 166<br>7.10 Dynamic Binding and Duck Typing 167<br>7.11 The Danger of Inheriting from Built-in Types 167<br>7.12 Class Variables and Methods 169<br>7.13 Static Methods 173<br>7.14 A Word about Design Patterns 176<br>7.15 Data Encapsulation and Private Attributes 176<br>7.16 Type Hinting 179<br>7.17 Properties 180<br>7.18 Types, Interfaces, and Abstract Base Classes 183<br>7.19 Multiple Inheritance, Interfaces, and Mixins 187<br>7.20 Type-Based Dispatch 193<br>7.21 Class Decorators 194<br>7.22 Supervised Inheritance 197<br>7.23 The Object Life Cycle and Memory Management 199<br>7.24 Weak References 204<br>7.25 Internal Object Representation and Attribute Binding 206<br>7.26 Proxies, Wrappers, and Delegation 208<br>7.27 Reducing Memory Use with __slots__ 210<br>7.28 Descriptors 211<br>7.29 Class Definition Process 215<br>7.30 Dynamic Class Creation 216<br>7.31 Metaclasses 217<br>7.32 Built-in Objects for Instances and Classes 222<br>7.33 Final Words: Keep It Simple 223<br><strong>Chapter 8: Modules and Packages 225</strong><br>8.1 Modules and the import Statement 225<br>8.2 Module Caching 227<br>8.3 Importing Selected Names from a Module 228<br>8.4 Circular Imports 230<br>8.5 Module Reloading and Unloading 232<br>8.6 Module Compilation 233<br>8.7 The Module Search Path 234<br>8.8 Execution as the Main Program 234<br>8.9 Packages 235<br>8.10 Imports Within a Package 237<br>8.11 Running a Package Submodule as a Script 238<br>8.12 Controlling the Package Namespace 239<br>8.13 Controlling Package Exports 240<br>8.14 Package Data 241<br>8.15 Module Objects 242<br>8.16 Deploying Python Packages 243<br>8.17 The Penultimate Word: Start with a Package 244<br>8.18 The Final Word: Keep It Simple 245<br><strong>Chapter 9: Input and Output 247</strong><br>9.1 Data Representation 247<br>9.2 Text Encoding and Decoding 248<br>9.3 Text and Byte Formatting 250<br>9.4 Reading Command-Line Options 254<br>9.5 Environment Variables 256<br>9.6 Files and File Objects 256<br>9.7 I/O Abstraction Layers 260<br>9.8 Standard Input, Output, and Error 263<br>9.9 Directories 264<br>9.10 The print() function 265<br>9.11 Generating Output 265<br>9.12 Consuming Input 266<br>9.13 Object Serialization 268<br>9.14 Blocking Operations and Concurrency 269<br>9.15 Standard Library Modules 273<br>9.16 Final Words 296<br><strong>Chapter 10: Built-in Functions and Standard Library 297</strong><br>10.1 Built-in Functions 297<br>10.2 Built-in Exceptions 314<br>10.3 Standard Library 318<br>10.4 Final Words: Use the Built-Ins 320<br>Index 321</p>