Preface ix
1. Introduction to Python 1
The Python Language 1
The Python Standard Library and Extension Modules 2
Python Implementations 3
Python Development and Versions 10
Python Resources 11
Installation 14
Installing Python from Binaries 15
Installing Python from Source Code 16
2. The Python Interpreter 21
The python Program 21
Python Development Environments 27
Running Python Programs 29
Running Python in the Browser 30
3. The Python Language 33
Lexical Structure 33
Data Types 40
Variables and Other References 52
Expressions and Operators 57
Numeric Operations 60
Sequence Operations 62
Set Operations 69
Dictionary Operations 71
Control Flow Statements 73
Functions 93
4. Object-Oriented Python 115
Classes and Instances 115
Special Methods 141
Decorators 157
Metaclasses 158
5. Type Annotations 171
History 171
Type-Checking Utilities 173
Type Annotation Syntax 174
The typing Module 176
Using Type Annotations at Runtime 191
How to Add Type Annotations to Your Code 192
Summary 194
6. Exceptions 195
The try Statement 195
The raise Statement 200
The with Statement and Context Managers 201
Generators and Exceptions 203
Exception Propagation 204
Exception Objects 205
Custom Exception Classes 210
ExceptionGroup and except* 212
Error-Checking Strategies 214
The assert Statement 219
7. Modules and Packages 221
Module Objects 222
Module Loading 227
Packages 233
Distribution Utilities (distutils) and setuptools 235
Python Environments 237
8. Core Built-ins and Standard Library Modules 247
Built-in Types 247
Built-in Functions 251
The sys Module 259
The copy Module 263
The collections Module 264
The functools Module 269
The heapq Module 271
The argparse Module 274
The itertools Module 275
9. Strings and Things 281
Methods of String Objects 281
The string Module 286
String Formatting 287
Text Wrapping and Filling 300
The pprint Module 300
The reprlib Module 301
Unicode 301
10. Regular Expressions 305
Regular Expressions and the re Module 305
Optional Flags 311
Match Versus Search 313
Anchoring at String Start and End 313
Regular Expression Objects 314
Match Objects 317
Functions of the re Module 318
REs and the := Operator 319
The Third-Party regex Module 320
11. File and Text Operations 321
The io Module 322
The tempfile Module 328
Auxiliary Modules for File I/O 330
In-Memory Files: io.StringIO and io.BytesIO 334
Archived and Compressed Files 335
The os Module 343
The errno Module 357
The pathlib Module 357
The stat Module 362
The filecmp Module 363
The fnmatch Module 365
The glob Module 366
The shutil Module 366
Text Input and Output 368
Richer-Text I/O 371
Internationalization 374
12. Persistence and Databases 383
Serialization 384
DBM Modules 396
The Python Database API (DBAPI) 399
13. Time Operations 411
The time Module 411
The datetime Module 415
The zoneinfo Module 422
The dateutil Module 424
The sched Module 425
The calendar Module 426
14. Customizing Execution 429
Per-Site Customization 429
Termination Functions 430
Dynamic Execution and exec 430
Internal Types 434
Garbage Collection 435
15. Concurrency: Threads and Processes 443
Threads in Python 445
The threading Module 446
The queue Module 456
The multiprocessing Module 459
The concurrent.futures Module 468
Threaded Program Architecture 471
Process Environment 475
Running Other Programs 476
The mmap Module 481
16. Numeric Processing 485
Floating-Point Values 485
The math and cmath Modules 488
The statistics Module 493
The operator Module 493
Random and Pseudorandom Numbers 496
The fractions Module 498
The decimal Module 500
Array Processing 502
17. Testing, Debugging, and Optimizing 513
Testing 514
Debugging 528
The warnings Module 538
Optimization 541
18. Networking Basics 563
The Berkeley Socket Interface 564
Transport Layer Security 579
SSLContext 580
19. Client-Side Network Protocol Modules 583
Email Protocols 584
HTTP and URL Clients 587
Other Network Protocols 595
20. Serving HTTP 597
http.server 598
WSGI 598
Python Web Frameworks 600
21. Email, MIME, and Other Network Encodings 611
MIME and Email Format Handling 611
Encoding Binary Data as ASCII Text 619
22. Structured Text: HTML 623
The html.entities Module 624
The BeautifulSoup Third-Party Package 624
Generating HTML 635
23. Structured Text: XML 643
ElementTree 644
Parsing XML with ElementTree.parse 650
Building an ElementTree from Scratch 652
Parsing XML Iteratively 652
24. Packaging Programs and Extensions 655
What We Don’t Cover in This Chapter 656
A Brief History of Python Packaging 657
Online Material 658
25. Extending and Embedding Classic Python 659
Online Material 660
26. v3.7 to v3.n Migration 661
Significant Changes in Python Through 3.11 662
Planning a Python Version Upgrade 664
Summary 668
Appendix. New Features and Changes in Python 3.7 Through 3.11 669
Index 687
Python was recently ranked as today's most popular programming language on the TIOBE index, thanks to its broad applicability to design and prototyping to testing, deployment, and maintenance. With this updated fourth edition, you'll learn how to get the most out of Python, whether you're a professional programmer or someone who needs this language to solve problems in a particular field.
Carefully curated by recognized experts in Python, this new edition focuses on version 3.10, bringing this seminal work on the Python language fully up to date on five version releases, including preview coverage of upcoming 3.11 features.