| Preface | xv |
Part I. | Getting Started | |
1. | A Python Q&A Session | 3 |
| Why Do People Use Python? | 3 |
| Is Python a Scripting Language? | 5 |
| Okay, But What's the Downside? | 6 |
| Who Uses Python Today? | 7 |
| What Can I Do with Python? | 8 |
| What Are Python's Technical Strengths? | 11 |
| How Does Python Stack Up to Language X? | 14 |
2. | How Python Runs Programs | 16 |
| Introducing the Python Interpreter | 16 |
| Program Execution | 17 |
| Execution Model Variations | 21 |
3. | How You Run Programs | 25 |
| Interactive Coding | 25 |
| System Command Lines and Files | 28 |
| Clicking Windows File Icons | 32 |
| Module Imports and Reloads | 36 |
| The IDLE User Interface | 40 |
| Other IDEs | 44 |
| Embedding Calls | 46 |
| Frozen Binary Executables | 47 |
| Text Editor Launch Options | 47 |
| Other Launch Options | 47 |
| Future Possibilities? | 47 |
| Which Option Should I Use? | 48 |
| Part I Exercises | 48 |
Part II. | Types and Operations | |
4. | Numbers | 53 |
| Python Program Structure | 53 |
| Why Use Built-in Types? | 53 |
| Numbers | 55 |
| Python Expression Operators | 57 |
| Numbers in Action | 60 |
| The Dynamic Typing Interlude | 68 |
5. | Strings | 74 |
| String Literals | 75 |
| Strings in Action | 81 |
| String Formatting | 87 |
| String Methods | 90 |
| General Type Categories | 95 |
6. | Lists and Dictionaries | 97 |
| Lists | 97 |
| Lists in Action | 99 |
| Dictionaries | 103 |
| Dictionaries in Action | 105 |
7. | Tuples, Files, and Everything Else | 112 |
| Tuples | 112 |
| Files | 115 |
| Type Categories Revisited | 117 |
| Object Generality | 118 |
| References Versus Copies | 118 |
| Comparisons, Equality, and Truth | 121 |
| Python's Type Hierarchies | 123 |
| Other Types in Python | 125 |
| Built-in Type Gotchas | 125 |
| Part II Exercises | 127 |
Part III. | Statements and Syntax | |
8. | Assignment, Expressions, and Print | 133 |
| Assignment Statements | 134 |
| Expression Statements | 140 |
| Print Statements | 142 |
9. | if Tests | 146 |
| if Statements | 146 |
| Python Syntax Rules | 149 |
| Truth Tests | 152 |
10. | while and for Loops | 155 |
| while Loops | 155 |
| break, continue, pass, and the Loop else | 156 |
| for Loops | 160 |
| Loop Variations | 164 |
11. | Documenting Python Code | 171 |
| The Python Documentation Interlude | 171 |
| Common Coding Gotchas | 182 |
| Part III Exercises | 184 |
Part IV. | Functions | |
12. | Function Basics | 189 |
| Why Use Functions? | 189 |
| Coding Functions | 190 |
| A First Example: Definitions and Calls | 193 |
| A Second Example: Intersecting Sequences | 194 |
13. | Scopes and Arguments | 197 |
| Scope Rules | 197 |
| The global Statement | 202 |
| Scopes and Nested Functions | 203 |
| Passing Arguments | 207 |
| Special Argument Matching Modes | 210 |
14. | Advanced Function Topics | 219 |
| Anonymous Functions: lambda | 219 |
| Applying Functions to Arguments | 224 |
| Mapping Functions Over Sequences | 227 |
| Functional Programming Tools | 228 |
| List Comprehensions | 229 |
| Generators and Iterators | 233 |
| Function Design Concepts | 236 |
| Function Gotchas | 239 |
| Part IV Exercises | 242 |
Part V. | Modules | |
15. | Modules: The Big Picture | 247 |
| Why Use Modules? | 247 |
| Python Program Architecture | 248 |
| How Imports Work | 251 |
16. | Module Coding Basics | 257 |
| Module Creation | 257 |
| Module Usage | 258 |
| Module Namespaces | 261 |
| Reloading Modules | 266 |
17. | Module Packages | 270 |
| Package Import Basics | 270 |
| Package Import Example | 273 |
| Why Use Package Imports? | 275 |
| A Tale of Three Systems | 275 |
18. | Advanced Module Topics | 279 |
| Data Hiding in Modules | 279 |
| Enabling Future Language Features | 280 |
| Mixed Usage Modes: __name__ and __main__ | 280 |
| Changing the Module Search Path | 281 |
| The import as Extension | 282 |
| Module Design Concepts | 282 |
| Module Gotchas | 285 |
| Part V Exercises | 292 |
Part VI. | Classes and OOP | |
19. | OOP: The Big Picture | 297 |
| Why Use Classes? | 297 |
| OOP from 30,000 Feet | 299 |
20. | Class Coding Basics | 307 |
| Classes Generate Multiple Instance Objects | 307 |
| Classes Are Customized by Inheritance | 310 |
| Classes Can Intercept Python Operators | 314 |
21. | Class Coding Details | 317 |
| The Class Statement | 317 |
| Methods | 320 |
| Inheritance | 322 |
| Operator Overloading | 327 |
| Namespaces: The Whole Story | 337 |
22. | Designing with Classes | 343 |
| Python and OOP | 343 |
| Classes as Records | 344 |
| OOP and Inheritance: "is-a" Relationships | 346 |
| OOP and Composition: "has-a" Relationships | 348 |
| OOP and Delegation | 352 |
| Multiple Inheritance | 353 |
| Classes Are Objects: Generic Object Factories | 356 |
| Methods Are Objects: Bound or Unbound | 358 |
| Documentation Strings Revisited | 360 |
| Classes Versus Modules | 362 |
23. | Advanced Class Topics | 363 |
| Extending Built-in Types | 363 |
| Pseudo-Private Class Attributes | 366 |
| "New Style" Classes in Python 2.2 | 369 |
| Class Gotchas | 376 |
| Part VI Exercises | 384 |
Part VII. | Exceptions and Tools | |
24. | Exception Basics | 393 |
| Why Use Exceptions? | 393 |
| Exception Handling: The Short Story | 395 |
| The try/except/else Statement | 399 |
| The try/finally Statement | 404 |
| The raise Statement | 405 |
| The assert Statement | 408 |
25. | Exception Objects | 410 |
| String-Based Exceptions | 410 |
| Class-Based Exceptions | 411 |
| General raise Statement Forms | 418 |
26. | Designing with Exceptions | 420 |
| Nesting Exception Handlers | 420 |
| Exception Idioms | 423 |
| Exception Design Tips | 426 |
| Exception Gotchas | 430 |
| Core Language Summary | 432 |
| Part VII Exercises | 436 |
Part VIII. | The Outer Layers | |
27. | Common Tasks in Python | 439 |
| Conversions, Numbers, and Comparisons | 443 |
| Manipulating Strings | 447 |
| Data Structure Manipulations | 452 |
| Manipulating Files and Directories | 458 |
| Internet-Related Modules | 473 |
| Executing Programs | 477 |
| Debugging, Testing, Timing, Profiling | 480 |
| Exercises | 483 |
28. | Frameworks | 485 |
| An Automated Complaint System | 486 |
| Interfacing with COM: Cheap Public Relations | 492 |
| A Tkinter-Based GUI Editor for Managing Form Data | 496 |
| Jython: The Felicitous Union of Python and Java | 504 |
| Exercises | 511 |
29. | Python Resources | 513 |
| Layers of Community | 513 |
| The Process | 517 |
| Services and Products | 518 |
| The Legal Framework: The Python Software Foundation | 518 |
| Software | 518 |
| Popular Third-Party Software | 520 |
| Web Application Frameworks | 529 |
| Tools for Python Developers | 531 |
Part IX. | Appendixes | |
A. | Installation and Configuration | 535 |
B. | Solutions to Exercises | 541 |
| Index | 577 |