Programming in Python 3 A Complete Introduction to the Python Language Mark Summerfield.4.Addison-Wesley Upper Saddle River, NJ Boston Indianapolis San Francisco New York Toronto Montreal London Munich Paris Madrid Capetown Sydney Tokyo Singapore Mexico City
Contents Introduction 1 Chapter I. Rapid Introduction to Procedural Programming 7 Creating and Running Python Programs 7 Python's "Beautiful Heart" 12 Piece #1: Data Types 12 Piece #2: Object References 14 Piece #3: Collection Data Types 16 Piece #4: Logical Operations 19 Piece #5: Control Flow Statements 23 Piece #6: Arithmetic Operators 28 Piece #7: Input/Output 31 Piece #8: Creating and Calling Functions 34 Examples 36 bigdigits.py 36 generate_grid.py 39 Summary 42 Exercises 44 Chapter 2. Data Types 47 Identifiers and Keywords 47 Integral Types 50 Integers 51 Booleans 53 Floating-Point Types 54 Floating-Point Numbers 55 Complex Numbers 58 Decimal Numbers 59 Strings 61 Comparing Strings 63 Slicing and Striding Strings 64 String Operators and Methods 67 vii
String Formatting with the str.formato Method 74 Character Encodings 85 Examples 88 quadratic.py 88 csv2html.py 90 Summary 95 Exercises 97 Chapter 3. Collection Data Types 99 Sequence Types 99 Tuples 100 Named Tuples 103 Lists 104 Set Types 112 Sets 112 Frozen Sets 116 Mapping Types 117 Dictionaries 118 Default Dictionaries 126 Iterating and Copying Collections 127 Iterators and Iterable Operations and Functions 128 Copying Collections 136 Examples 138 generate_usernames.py 138 statistics.py 142 Summary 146 Exercises 147 Chapter 4. Control Structures and Functions 149 Control Structures 149 Conditional Branching 149 Looping 151 Exception Handling 153 Catching and Raising Exceptions 153 Custom Exceptions 158 Custom Functions 161 Names and Docstrings 166 Argument and Parameter Unpacking 167 Accessing Variables in the Global Scope 170 viii
Lambda Functions 172 Assertions 173 Example: make_html_skeleton.py 175 Summary 181 Exercise 182 Chapter 5. Modules 185 Modules and Packages 185 Packages 189 Custom Modules 192 Overview of Python's Standard Library 202 String Handling 202 Command-Line Programming 204 Mathematics and Numbers 205 Times and Dates 205 Algorithms and Collection Data Types 207 File Formats, Encodings, and Data Persistence 208 File, Directory, and Process Handling 211 Networking and Internet Programming 214 XML 216 Other Modules 217 Summary 219 Exercise 220 Chapter 6. Object-Oriented Programming 223 The Object-Oriented Approach 224 Object-Oriented Concepts and Terminology 225 Custom Classes 228 Attributes and Methods 228 Inheritance and Polymorphism 233 Using Properties to Control Attribute Access 236 Creating Complete and Fully Integrated Data Types 238 Custom Collection Classes 251 Creating Classes That Aggregate Collections 251 Creating Collection Classes Using Aggregation 258 Creating Collection Classes Using Inheritance 265 Summary 272 Exercises 274 ix
Chapter 7. File Handling 277 Writing and Reading Binary Data 282 Pickles with Optional Compression 282 Raw Binary Data with Optional Compression 285 Writing and Parsing Text Files 294 Writing Text 295 Parsing Text 296 Parsing Text Using Regular Expressions 299 Writing and Parsing XML Files 302 Element Trees 302 DOM (Document Object Model) 306 Manually Writing XML 309 Parsing XML with SAX (Simple API for XML) 310 Random Access Binary Files 313 A Generic BinaryRecordFile Class 314 Example: The BikeStock Module's Classes 322 Summary 326 Exercises 327 Chapter 8. Advanced Programming Techniques 329 Further Procedural Programming 330 Branching Using Dictionaries 330 Generator Expressions and Functions 331 Dynamic Code Execution and Dynamic Imports 334 Local and Recursive Functions 341 Function and Method Decorators 346 Function Annotations 350 Further Object-Oriented Programming 353 Controlling Attribute Access 353 Functors 357 Context Managers 359 Descriptors 362 Class Decorators 367 Abstract Base Classes 370 Multiple Inheritance 377 Metaclasses 380 Functional-Style Programming 384 Partial Function Application 387
Example: Valid.py 388 Summary 390 Exercises 392 Chapter 9. Processes and Threading 395 Delegating Work to Processes 396 Delegating Work to Threads 400 Example: A Threaded Find Word Program 401 Example: A Threaded Find Duplicate Files Program 404 Summary 409 Exercises 410 Chapter 10. Networking 413 Creating a TCP Client 414 Creating a TCP Server 420 Summary 427 Exercises 427 Chapter 11. Database Programming 431 DBM Databases 432 SQL Databases 436 Summary 443 Exercise 444 Chapter 12. Regular Expressions 445 Python's Regular Expression Language 446 Characters and Character Classes 446 Quantifiers 447 Grouping and Capturing 450 Assertions and Flags 451 The Regular Expression Module 455 Summary 464 Exercises 465 Chapter 13. Introduction to GUI Programming 467 Dialog-Style Programs 470 Main-Window-Style Programs 476 Creating a Main Window 477 Creating a Custom Dialog 488 Summary 491 xi
Exercises 491 Epilogue 493 Index 495 xli