Mark Pilgrim's Dive Into Python 3 is a hands-on guide to Python 3 (the latest version of the Python language) and its differences from Python 2. As in the original book, Dive Into Python, each chapter starts with a real, complete code sample, proceeds to pick it apart and explain the pieces, and then puts it all back together in a summary at the end. Dive Into Python 3 is a Python book for experienced programmers. Whether you're an experienced programmer looking to get into Python 3 or grizzled Python veteran who remembers the days when you had to import the string module, Dive Into Python 3 is your 'desert island' Python 3 book. If you've never programmed before, Python 3 is an excellent language to learn modern programming techniques. But this book should not be your starting point. Get "How to Think Like a Computer Scientist: Learning with Python" by Allen Downey, Jeffrey Elkner, Chris Meyers and learn the basics. Then dive into this book. Dive Into PYTHON 3 was written by Mark Pilgram, and this edition is distributed under the terms of the Creative Commons Attribution Share-Alike License 3.0. * Money raised from the sale of this book supports the development of free software and documentation. About the Author: By day, Mark Pilgrim is a developer advocate for open source and open standards. By night, he is a husband and father who lives in North Carolina with his wife, his two sons, and his big slobbery dog. He spends his copious free time sunbathing, skydiving, and making up autobiographical information.
Author(s): Mark Pilgrim
Publisher: CreateSpace
Year: 2010
Language: English
Pages: 500
a.k.a. “the minus level”......Page 1
Which Python Is Right For You?......Page 3
Installing on Microsoft Windows......Page 4
Installing on Mac OS X......Page 12
Installing on Ubuntu Linux......Page 22
Using The Python Shell......Page 29
Python Editors and IDEs......Page 33
Diving In......Page 34
Declaring Functions......Page 36
Optional and Named Arguments......Page 38
Documentation Strings......Page 40
The import Search Path......Page 41
Everything Is An Object......Page 43
What’s An Object?......Page 44
Indenting Code......Page 45
Exceptions......Page 46
Catching Import Errors......Page 48
Unbound Variables......Page 49
Everything is Case-Sensitive......Page 50
Running Scripts......Page 51
Further Reading......Page 52
Diving In......Page 53
Booleans......Page 54
Coercing Integers To Floats And Vice-Versa......Page 56
Common Numerical Operations......Page 57
Trigonometry......Page 59
Numbers In A Boolean Context......Page 60
Creating A List......Page 62
Slicing A List......Page 63
Adding Items To A List......Page 64
Searching For Values In A List......Page 67
Removing Items From A List......Page 68
Removing Items From A List: Bonus Round......Page 69
Tuples......Page 71
Tuples In A Boolean Context......Page 74
Assigning Multiple Values At Once......Page 75
Creating A Set......Page 76
Modifying A Set......Page 78
Removing Items From A Set......Page 79
Common Set Operations......Page 81
Sets In A Boolean Context......Page 84
Creating A Dictionary......Page 85
Modifying A Dictionary......Page 86
Mixed-Value Dictionaries......Page 87
None......Page 89
None In A Boolean Context......Page 90
Further Reading......Page 91
Working With Files And Directories......Page 92
The Current Working Directory......Page 93
Working With Filenames and Directory Names......Page 94
Listing Directories......Page 96
Getting File Metadata......Page 98
Constructing Absolute Pathnames......Page 99
List Comprehensions......Page 100
Dictionary Comprehensions......Page 102
Other Fun Stuff To Do With Dictionary Comprehensions......Page 104
Set Comprehensions......Page 105
Further Reading......Page 106
Some Boring Stuff You Need To Understand Before You Can Dive In......Page 107
Unicode......Page 110
Diving In......Page 112
Formatting Strings......Page 113
Compound Field Names......Page 116
Format Specifiers......Page 118
Other Common String Methods......Page 119
Slicing A String......Page 121
Strings vs. Bytes......Page 122
Further Reading......Page 127
Diving In......Page 129
Case Study: Street Addresses......Page 130
Case Study: Roman Numerals......Page 132
Checking For Thousands......Page 134
Checking For Hundreds......Page 135
Using The {n,m} Syntax......Page 137
Checking For Tens And Ones......Page 138
Verbose Regular Expressions......Page 142
Case study: Parsing Phone Numbers......Page 144
Summary......Page 150
Diving In......Page 152
I Know, Let’s Use Regular Expressions!......Page 153
A List Of Functions......Page 157
A List Of Patterns......Page 161
A File Of Patterns......Page 163
Generators......Page 165
A Fibonacci Generator......Page 167
A Plural Rule Generator......Page 168
Further Reading......Page 170
Diving In......Page 171
Defining Classes......Page 172
The __init__() Method......Page 173
Instantiating Classes......Page 174
Instance Variables......Page 175
A Fibonacci Iterator......Page 177
A Plural Rule Iterator......Page 179
Further Reading......Page 186
Diving In......Page 188
Finding all occurrences of a pattern......Page 191
Finding the unique items in a sequence......Page 192
Making assertions......Page 194
Generator expressions......Page 195
Calculating Permutations… The Lazy Way!......Page 196
Other Fun Stuff in the itertools Module......Page 200
A New Kind Of String Manipulation......Page 205
Evaluating Arbitrary Strings As Python Expressions......Page 207
Putting It All Together......Page 212
Further Reading......Page 213
(Not) Diving In......Page 214
A Single Question......Page 216
“Halt And Catch Fire”......Page 224
More Halting, More Fire......Page 229
And One More Thing…......Page 233
A Pleasing Symmetry......Page 237
More Bad Input......Page 243
Diving In......Page 248
Handling Changing Requirements......Page 252
Refactoring......Page 259
Summary......Page 265
Reading From Text Files......Page 266
Character Encoding Rears Its Ugly Head......Page 267
Stream Objects......Page 268
Reading Data From A Text File......Page 269
Closing Files......Page 272
Closing Files Automatically......Page 273
Reading Data One Line At A Time......Page 274
Writing to Text Files......Page 276
Binary Files......Page 278
Stream Objects From Non-File Sources......Page 281
Handling Compressed Files......Page 283
Standard Input, Output, and Error......Page 285
Redirecting Standard Output......Page 286
Further Reading......Page 290
Diving In......Page 291
A 5-Minute Crash Course in XML......Page 294
The Structure Of An Atom Feed......Page 296
Parsing XML......Page 299
Elements Are Lists......Page 300
Attributes Are Dictonaries......Page 301
Searching For Nodes Within An XML Document......Page 302
Going Further With lxml......Page 306
Generating XML......Page 309
Parsing Broken XML......Page 312
Further Reading......Page 314
Diving In......Page 315
Saving Data to a Pickle File......Page 316
Loading Data from a Pickle File......Page 318
Pickling Without a File......Page 320
Bytes and Strings Rear Their Ugly Heads Again......Page 321
Debugging Pickle Files......Page 322
Serializing Python Objects to be Read by Other Languages......Page 325
Saving Data to a JSON File......Page 326
Mapping of Python Datatypes to JSON......Page 328
Serializing Datatypes Unsupported by JSON......Page 329
Loading Data from a JSON File......Page 334
Further Reading......Page 338
Diving In......Page 340
Caching......Page 341
Last-Modified Checking......Page 343
ETag Checking......Page 345
Redirects......Page 347
How Not To Fetch Data Over HTTP......Page 349
What’s On The Wire?......Page 350
Introducing httplib2......Page 355
A Short Digression To Explain Why httplib2 Returns Bytes Instead of Strings......Page 359
How httplib2 Handles Caching......Page 360
How httplib2 Handles Last-Modified and ETag Headers......Page 364
How http2lib Handles Compression......Page 367
How httplib2 Handles Redirects......Page 368
Beyond HTTP GET......Page 374
Beyond HTTP POST......Page 380
Further Reading......Page 382
Diving In......Page 384
Introducing The chardet Module......Page 385
UTF-n With A BOM......Page 386
Multi-Byte Encodings......Page 387
Single-Byte Encodings......Page 388
Running 2to3......Page 389
A Short Digression Into Multi-File Modules......Page 395
False is invalid syntax......Page 397
No module named constants......Page 398
Name 'file' is not defined......Page 400
Can’t use a string pattern on a bytes-like object......Page 401
Can't convert 'bytes' object to str implicitly......Page 404
Unsupported operand type(s) for +: 'int' and 'bytes'......Page 408
ord() expected string of length 1, but int found......Page 410
Unorderable types: int() >= str()......Page 413
Global name 'reduce' is not defined......Page 417
Summary......Page 421
Diving In......Page 422
Things Distutils Can’t Do For You......Page 424
Directory Structure......Page 425
Writing Your Setup Script......Page 427
Classifying Your Package......Page 428
Examples of Good Package Classifiers......Page 429
Specifying Additional Files With A Manifest......Page 431
Checking Your Setup Script for Errors......Page 432
Creating a Source Distribution......Page 433
Creating a Graphical Installer......Page 436
Building Installable Packages for Other Operating Systems......Page 438
Adding Your Software to The Python Package Index......Page 439
The Many Possible Futures of Python Packaging......Page 441
Further Reading......Page 442
print statement......Page 443
unicode() global function......Page 444
<> comparison......Page 445
Dictionary methods that return lists......Page 446
http......Page 447
urllib......Page 448
dbm......Page 449
Other modules......Page 450
Relative imports within a package......Page 451
next() iterator method......Page 452
filter() global function......Page 453
reduce() global function......Page 454
intern() global function......Page 455
execfile statement......Page 456
try...except statement......Page 457
raise statement......Page 458
xrange() global function......Page 459
raw_input() and input() global functions......Page 460
xreadlines() I/O method......Page 461
lambda functions that take a tuple instead of multiple parameters......Page 462
__nonzero__ special method......Page 463
callable() global function......Page 464
types module constants......Page 465
isinstance() global function......Page 466
itertools module......Page 467
os.getcwdu() function......Page 468
set() literals (explicit)......Page 469
Whitespace around commas (explicit)......Page 470
Common idioms (explicit)......Page 471
Basics......Page 472
Classes That Act Like Iterators......Page 473
Computed Attributes......Page 474
Classes That Act Like Functions......Page 477
Classes That Act Like Sequences......Page 479
Classes That Act Like Dictionaries......Page 480
Classes That Act Like Numbers......Page 482
Classes That Can Be Compared......Page 485
Classes That Can Be Serialized......Page 486
Classes That Can Be Used in a with Block......Page 487
Further Reading......Page 489
Things to Read......Page 491
Where To Look For Python 3-Compatible Code......Page 492