Cover....1
Copyright....6
Table of Contents....9
Preface....27
Audience....28
Changes in the Third Edition....28
Outline....28
Python Versions....32
About the Author....32
Conventions Used in This Book....33
Using Code Examples....34
O’Reilly Online Learning....34
How to Contact Us....35
Acknowledgments....35
Part I. Stronghold....37
Chapter 1. Introduction....39
Mysteries....39
Little Python Programs....42
Setup....44
Install Python....45
Upgrade Python....45
Run Python Programs....45
The Python Interactive Interpreter....46
Python Files....46
Built-In Python Features....47
The Python Standard Library....47
Third-Party Python Packages....47
A Bigger Example....47
Review/Preview....50
Chapter 2. Types and Variables....51
A Computer....51
Bits and Bytes....52
Multibyte Types....55
Variables....57
Assign a Value to a Variable....57
Change the Value of a Variable....60
Delete a Variable....61
Name Variables....62
Follow Naming Conventions....63
Python Types....63
Specify Values....64
Objects as Plastic Boxes in Memory....65
Review/Preview....65
Practice....65
Chapter 3. Numbers....67
Booleans....67
Integers....68
Literal Integers....69
Integer Operations....70
Integers and Variables....72
Precedence....74
Bases....75
Type Conversions....77
How Big Is an int?....79
Floats....80
Floats Are Not Exact....82
Fractions....82
Decimals....83
Math Functions....83
Review/Preview....83
Practice....83
Chapter 4. Strings....85
Create with Quotes....86
Create with str()....88
Escape with \....89
Combine with +....90
Duplicate with *....91
Get a Character by [ offset ]....91
Get a Substring with a Slice....92
Get Length with len()....94
Split with split()....95
Combine with join()....95
Substitute with replace()....95
Work with Prefixes and Suffixes....96
Strip with strip()....97
Search and Select....98
Change Case....99
Set Alignment....100
Apply Formatting....100
Old style: %....101
New style: {} and format()....104
Newest Style: f-strings....105
Review/Preview....106
Practice....107
Chapter 5. Bytes and Bytearray....109
Bytes....109
Create with Quotes....110
Create with bytes()....110
Create from a Hex String....111
Decode and Encode Bytes and Strings....112
Convert to a Hex String....112
Get One Byte by [ offset ]....112
Get a Slice....112
Combine with +....113
Repeat with *....113
Bytearray....113
Create with bytearray()....113
Get One Byte by [ offset ]....114
Get Multiple Bytes with a Slice....114
Modify One Byte by [ offset ]....114
Modify Multiple Bytes with replace()....114
Modify Multiple Bytes with a Slice....115
Insert a Byte with insert()....115
Append One Byte with append()....115
Append Multiple Bytes with extend()....115
Combine with +....116
Repeat with *....116
Review/Preview....116
Practice....116
Chapter 6. If and Match....117
Comment with #....117
Continue Lines with \....118
Compare with if, elif, and else....119
What Is True?....122
Do Multiple Comparisons with in....123
New: I Am the Walrus....124
Match....125
Simple Matches....126
Structural Matches....127
Review/Preview....127
Practice....128
Chapter 7. For and While....129
Repeat with while....129
Cancel with break....130
Skip Ahead with continue....130
Check break Use with else....131
Iterate with for and in....131
Cancel with break....132
Skip with continue....132
Check break Use with else....132
Generate Number Sequences with range()....133
Review/Preview....134
Practice....134
Chapter 8. Tuples and Lists....135
Tuples....136
Create with Commas and ()....136
Create with tuple()....138
Get an Item by [ offset ]....138
Combine with +....138
Duplicate with *....138
Compare....138
Iterate with for and in....139
Modify?....139
Lists....140
Create with []....140
Create or Convert with list()....140
Create from a String with split()....141
Get an Item by [ offset ]....141
Get Items with a Slice....142
Add an Item to the End with append()....143
Add an Item by Offset with insert()....143
Duplicate with *....144
Combine with extend() or +....144
Change an Item with [ offset ]....144
Change Items with a Slice....145
Delete an Item by Offset with del....145
Delete an Item by Value with remove()....146
Get an Item by Offset and Delete It with pop()....146
Delete All Items with clear()....147
Find an Item’s Offset by Value with index()....147
Test for a Value with in....147
Count Occurrences of a Value with count()....148
Convert a List to a String with join()....148
Reorder Items with sort() or sorted()....149
Get Length with len()....149
Assign with =....150
Copy with copy(), list(), or a Slice....150
Copy Everything with deepcopy()....151
Compare Lists....152
Iterate with for and in....152
Iterate Multiple Sequences with zip()....153
Iterate Multiple Sequences with zip_longest()....154
Create a List with a Comprehension....155
Create Lists of Lists....157
Tuples Versus Lists....158
There Are No Tuple Comprehensions....158
Review/Preview....159
Practice....159
Chapter 9. Dictionaries and Sets....161
Dictionaries....161
Create with {}....162
Create with dict()....162
Convert with dict()....163
Add or Change an Item by [ key ]....164
Get an Item by [ key ] or with get()....165
Iterate with for and in....166
Get Length with len()....167
Combine/update dicts....167
Delete an Item by Key with del....169
Get an Item by Key and Delete It with pop( key )....169
Delete All Items with clear()....170
Test for a Key with in....170
Assign with =....170
Copy with copy()....171
Copy Everything with deepcopy()....171
Compare Dictionaries....172
Use Dictionary Comprehensions....173
Sets....174
Create with set() or {}....174
Get Length with len()....175
Add an Item with add()....176
Delete an Item with remove()....176
Combine with |....176
Iterate with for and in....176
Test for a Value with in....177
Use Combinations and Operators....177
Create Set Comprehensions....181
Create an Immutable Set with frozenset()....181
Review/Preview....182
Practice....182
Chapter 10. Functions....185
Define a Function with def....185
Call a Function with Parentheses....186
Arguments and Parameters....187
None, Truthiness, and Falsiness....188
Positional Arguments....190
Keyword Arguments....191
Default Parameter Values....191
Pack/Unpack Positional Arguments with *....192
Pack/Unpack Keyword Arguments with **....194
Keyword-Only (*) and Position-Only (/) Arguments....195
Mutable and Immutable Arguments....197
Docstrings....198
Functions Are First-Class Citizens....198
Function Arguments Are Not a Tuple....201
Inner Functions....201
Closures....202
Anonymous Functions: Lambda....203
Generators....204
Generator Functions....204
Generator Comprehensions....206
Decorators....206
Namespaces and Scope....209
Dunder Names....211
Recursion....211
Async Functions, Briefly....213
Exceptions....213
Handle Errors with try and except....214
Use Finally....215
Make Your Own Exceptions....216
Review/Preview....216
Practice....217
Chapter 11. Objects....219
What Are Objects?....219
Simple Objects....220
Define a Class with class....220
Assign Attributes....221
Methods....222
Initialization....222
Inheritance....224
Inherit from a Parent Class....224
Override a Method....225
Add a Method....227
Get Help from Your Parent with super()....227
Use Multiple Inheritance....228
Include Mixins....230
In self Defense....231
Attribute Access....231
Direct Access....231
Getters and Setters....232
Properties for Attribute Access....233
Properties for Computed Values....234
Name Mangling for Privacy....235
Class and Object Attributes....236
Method Types....237
Instance Methods....237
Class Methods....237
Static Methods....238
Duck Typing....238
Magic Methods....241
Aggregation and Composition....244
When to Use Objects or Something Else....244
Named Tuples....245
Dataclasses....247
Attrs....248
Review/Preview....248
Practice....249
Chapter 12. Modules and Packages....251
Modules and the import Statement....251
Import a Module....252
Import a Module with Another Name....254
Import Only What You Want from a Module....254
Packages....254
The Module Search Path....256
Relative and Absolute Imports....257
Namespace Packages....257
Modules Versus Objects....258
Goodies in the Python Standard Library....259
Handle Missing Keys with setdefault() and defaultdict()....259
Count Items with Counter()....261
Order by Key with OrderedDict()....263
Stack + Queue == deque....263
Iterate over Code Structures with itertools....264
Get Random....266
More Batteries: Get Other Python Code....267
Review/Preview....267
Practice....267
Part II. Tools....269
Chapter 13. Development Environment....271
Find Python Code....271
Install Packages....272
Use Pip....272
Install with a Native Package Manager....273
Install from Source....274
Virtual Environments....274
Virtualenv and Venv....275
Pipenv....276
Poetry....276
Conda....276
uv....276
Integrated Development Environments....277
IPython....278
Jupyter Notebook....280
JupyterLab....280
Source Control....280
Mercurial....281
Git....281
Review/Preview....284
Practice....284
Chapter 14. Type Hints and Documentation....285
Type Hints....285
Variable Hints....286
Function Hints....287
Mypy....287
Documentation....288
Comments....289
Docstrings....289
Markup Text Files....289
Review/Preview....289
Practice....290
Chapter 15. Testing....291
Pylint....291
Ruff....293
Unittest....294
Doctest....298
Pytest....299
Examples....300
Fixtures....301
Parametrization....303
Hypothesis....304
Nox....305
Continuous Integration....305
Review/Preview....306
Practice....306
Chapter 16. Debugging....307
Assert....308
Print....309
F-strings....309
Pprint()....310
IceCream....310
Decorators....311
Logging....312
Pdb....315
Breakpoint()....320
Review/Preview....321
Practice....321
Part III. Quests....323
Chapter 17. Text Data....325
Text Strings: Unicode....325
Python Unicode Strings....326
UTF-8....329
Encode....330
Decode....331
HTML Entities....333
Normalization....334
Text Strings: Regular Expressions....335
Find Exact Beginning Match with match()....336
Find First Match with search()....338
Find All Matches with findall()....338
Split at Matches with split()....338
Replace at Matches with sub()....339
Patterns....339
Using Special Characters....339
Using Specifiers....341
Specifying match() Output....343
Review/Preview....344
Practice....344
Chapter 18. Binary Data....345
Convert Binary Data with struct....345
Extraction with Binary Data Tools....348
Convert Bytes/Strings with binascii()....349
Use Bit Operators....349
Review/Preview....350
Practice....350
Chapter 19. Dates and Times....351
Leap Year....352
The datetime Module....353
The time Module....355
Read and Write Dates and Times....357
All the Conversions....361
Alternative Modules....362
Review/Preview....362
Practice....363
Chapter 20. Files....365
File Input and Output....365
Create or Open with open()....366
Write a Text File with print()....367
Write a Text File with write()....367
Read a Text File with read(), readline(), or readlines()....369
Write a Binary File with write()....370
Read a Binary File with read()....371
Close Files Automatically by Using with....371
Change Position with seek()....371
Memory Mapping....373
File Operations....374
Check Existence with exists()....374
Check Type with isfile()....374
Copy with copy()....375
Change Name with rename()....375
Link with link() or symlink()....375
Change Permissions with chmod()....376
Change Ownership with chown()....376
Delete a File with remove()....376
Directory Operations....376
Create with mkdir()....377
Delete with rmdir()....377
List Contents with listdir()....377
Change Current Directory with chdir()....378
List Matching Files with glob()....378
Pathnames....379
Get a Pathname with abspath()....379
Get a symlink Pathname with realpath()....380
Build a Pathname with os.path.join()....380
Use pathlib....380
BytesIO and StringIO....381
File Formats: Determination....383
Review/Preview....383
Practice....383
Chapter 21. Data in Time: Concurrency....385
Programs and Processes....385
Create a Process with subprocess....386
Create a Process with multiprocessing....387
Kill a Process with terminate()....388
Get System Info with os....389
Get Process Info with psutil....389
Command Automation....390
Invoke....390
Other Command Helpers....391
Concurrency....392
Queues....393
Processes....393
Threads....395
The GIL....397
Concurrent.futures....398
Green Threads and gevent....401
Twisted....403
asyncio....405
Coroutines and Event Loops....406
Asyncio Alternatives....408
Async Versus…....409
Async Frameworks and Servers....410
Redis....411
Beyond Queues....414
Review/Preview....415
Practice....415
Chapter 22. Data in Space: Networks....417
TCP/IP....417
Sockets....418
Scapy....423
Netcat....423
Networking Patterns....424
The Request-Reply Pattern....424
Request-Reply: ZeroMQ....425
Request-Reply: Other Messaging Tools....429
The Publish-Subscribe Pattern....429
Pub-Sub: Redis....429
Pub-Sub: ZeroMQ....431
Pub-Sub: Other Tools....433
Internet Services....433
Domain Name System....433
Python Email Modules....434
Other Protocols....434
Web Services and APIs....435
Data Serialization....435
Serialize with pickle....436
Use Other Serialization Formats....437
Remote Procedure Calls....438
XML-RPC....438
JSON RPC....439
MessagePack RPC....440
zerorpc....441
gRPC....442
Remote Management....443
Big Fat Data....443
Hadoop....443
Spark....444
Disco....444
Dask....444
Clouds....444
Amazon Web Services....446
Google Cloud....446
Microsoft Azure....446
OpenStack....446
Docker....446
Kubernetes....447
Review/Preview....447
Practice....447
Chapter 23. Data in a Box: Persistent Storage....449
Text Files....449
Tabular and Delimited Text Files....450
CSV....450
XML....452
An XML Security Note....454
HTML....455
JSON....455
YAML....458
TOML....460
Tablib....460
Configuration Files....460
Binary Files....461
Padded Binary Files and Memory Mapping....461
Spreadsheets....461
HDF5....462
TileDB....462
Relational Databases....462
SQL....463
DB-API....465
SQLite....465
DuckDB....467
MySQL....467
PostgreSQL....468
SQLAlchemy....468
Other Database Access Packages....474
NoSQL Data Stores....475
The dbm Family....475
Memcached....476
Redis....477
Redis Alternative: Valkey?....484
Document Databases....484
Time-Series Databases....485
Graph Databases....485
Other NoSQL....486
Full-Text Databases....486
Vector Databases....486
Geospatial Databases....487
Review/Preview....487
Practice....487
Chapter 24. The Web....489
Web Basics....490
HTTP Testing....491
Telnet....491
curl....492
HTTPie....493
httpbin....494
Web Clients....494
The Standard Library....494
Requests....496
Other Web Clients....498
Web Servers....500
The Simplest Python Web Server....500
Web Server Gateway Interface....502
ASGI....502
Apache....503
NGINX....504
Other Python Web Servers....504
Web Server Frameworks....505
Bottle....506
Flask....508
Django....512
FastAPI....513
Litestar....513
Database Frameworks....514
Web Services and Automation....515
webbrowser....515
webview....515
Web APIs and REST....516
WebSockets....517
Webhooks....518
Web Frontends....518
htmx....519
FastHTML....519
Crawl and Scrape....520
Scrapy....520
Beautiful Soup....521
Requests-HTML....522
Let’s Watch a Movie....522
Review/Preview....525
Practice....525
Chapter 25. Data Science....527
Standard Python....528
Format Conversions....529
Math....529
Complex Numbers....531
Calculate Accurate Floating Point Values with decimal....532
Perform Rational Arithmetic with fractions....533
Use Packed Sequences with array....533
Statistics....533
Matrix Multiplication....534
NumPy....534
Make an Array with array()....534
Make an Array with arange()....535
Make an Array with zeros(), ones(), or random()....536
Change an Array’s Shape with reshape()....537
Get an Element with []....538
Array Math....539
Linear Algebra....539
SciPy....540
pandas....541
Polars....543
DuckDB....543
Data Visualization....544
Review/Preview....545
Practice....545
Chapter 26. AI....547
It Turns Out…....548
Expert Systems....548
Perceptrons....548
The Breakthrough....549
Image Recognition: ImageNet and AlexNet....549
Large Language Models....549
The ChatGPT Moment....550
Retrieval Augmented Generation....550
Agents....550
Efficiency....551
Create Models: Python Frameworks....551
Current Models....552
A Million Models: Hugging Face....552
Working Examples With Ollama....553
Install Ollama....553
Choose a Model....554
Choose Another Model....555
Ollama References....557
References....558
Review/Preview....558
Practice....559
Chapter 27. Performance....561
Computer Hardware....561
Measure Timing....563
Profile....567
Algorithms and Data Structures....568
Arrays Versus Lists and Tuples....569
Cache....569
Cython....570
NumPy and SciPy....570
C or Rust Extensions....570
Taichi....571
PyPy....571
Numba....571
Standard Python JIT....572
Mojo....572
Background....573
Design....574
Examples....574
Limitations....575
Conclusion....575
Review/Preview....575
Practice....576
Appendix. Practice Answers....577
2. Types and Variables....577
3. Numbers....578
4. Strings....579
5. Bytes and Bytearray....583
6. If and Match....584
7. For and While....585
8. Tuples and Lists....587
9. Dictionaries and Sets....591
10. Functions....595
11. Objects....597
12. Modules and Packages....601
13. Development Environment....603
14. Type Hints and Documentation....604
15. Testing....604
16. Debugging....607
17. Text Data....607
18. Binary Data....611
19. Dates and Times....612
20. Files....613
21. Data in Time: Concurrency....615
22. Data in Space: Networks....615
23. Data in a Box: Persistent Storage....621
24. The Web....625
25. Data Science....627
26. AI....628
27. Performance....634
Index....637
About the Author....659
Colophon....660
Stuck in a coding conundrum? Whether you're an advanced beginner, an intermediate developer, or a curious newcomer, the complexities of coding can often feel like a labyrinth with no exit. With Python, however, you can start writing real code quickly—but where should you start?
In this updated third edition, Bill Lubanovic acts as your personal guide to Python, offering a clear path through the intricacies and capabilities of this much-beloved coding language, including new chapters on AI models and performance enhancements. Easy to understand and enjoyable to read, this book not only teaches you the core concepts but also dives into practical applications that bridge the gap between learning and doing. By reading it, you will: