Professional Documents
Culture Documents
&
never
will
0.1. DIVING IN
B
0.2. WHICH PYTHON IS RIGHT FOR YOU?
versions
Register Extensions
Tcl/Tk
Documentation
Utility Scripts
Test Suite
Python 3.1
Mac Installer Disk Image
must
Python
Framework
GUI Applications
UNIX command-line tools
Python Documentation
Shell profile updater
Fix system Python
help mode
1.1. DIVING IN
modules
When you
need a
function,
just declare
it.
optional
i.e.
Every
function
deserves a
decent
docstring.
1.4. THE
SEARCH PATH
built-in modules
all
must
first-class objects
first-class objects
everything in Python is an
object
not
1.7. EXCEPTIONS
check
handle
unhandled
handled
if it exists
some
did
not
Everything
in Python is
an object.
2.1. DIVING IN
Booleans
Numbers
Strings
Bytes
e.g.
byte arrays e.g.
Lists
Tuples
Sets
Dictionaries
compiled code
&
&
2.2. BOOLEANS
virtually
any
expression
in a boolean
context.
2.3. NUMBERS
2.3.3. FRACTIONS
2.3.4. TRIGONOMETRY
Zero values
are false,
and nonzero values
are true.
2.4. LISTS
&
slicing
a_list[0] is
the first
item of
a_list.
four
is itself a list
where
first
not
Lists never
have gaps.
not
value
Empty lists
are false; all
other lists
are true.
2.5. TUPLES
can
immutable
2.6. SETS
promise
unordered
is
unique values
still
all
either
both
exactly one
looks
2.7. DICTIONARIES
is
Empty
dictionaries
are false; all
other
dictionaries
are true.
2.8.
2.8.1.
IN A BOOLEAN CONTEXT
CHAPTER 3. COMPREHENSIONS
Our imagination is stretched to the utmost, not, as in fiction, to imagine things which are not really there, but just
to comprehend those things which are.
Richard Feynman
3.1. DIVING IN
There is
always a
current
working
directory.
Do not
The
module uses
shell-like
wildcards.
i.e.
CHAPTER 4. STRINGS
Im telling you this cause youre one of my friends.
My alphabet starts where your alphabet ends!
Dr. Seuss, On Beyond Zebra!
F
!@#$%&
character encoding
Everything
you thought
you knew
about
strings is
wrong.
&
&
&
those
4.2. UNICODE
Enter Unicode.
every
every
this
guarantee
really
UTF-8
variable-length
&
4.3. DIVING IN
i.e.
Strings can
be defined
with either
single or
double
quotes.
&
{0} is
replaced by
the 1st
format()
Any combination of the above
argument.
{1} is
replaced by
the 2nd.
e.g.
e.g.
format specifier
i.e.
string
bytes
can never do
i.e.
5.1. DIVING IN
&
more
not
^ matches
the start of
a string. $
matches the
end of a
string.
really
and
raw string
*sigh*
? makes a
pattern
optional.
not
SYNTAX
{1,4}
matches
between 1
and 4
occurrences
of a pattern.
fails to match
(A|B)
matches
either
pattern A or
pattern B,
but not
both.
does
\d matches
any numeric
digit (09).
\D matches
anything
but digits.
and then remember them as a group that I can ask for later
without
except
without
5.7. SUMMARY
remembered group
6.1. DIVING IN
Bass
basses fax
faxes
coach
rashes
cheetahs
waltz
waltzes
coaches
cheetah
rash
vacancy
vacancies
Man
humans Mouse
wife
wives
sheep deer
mice
lowlife
louse
day
men
lice
house
days
woman
women
houses Knife
lowlifes
haiku
human
knives
Regular Expressions
all
except
not
The rules
variable is a
sequence of
pairs of
functions.
closures
strings
context
6.6. GENERATORS
that
generator
&
yield
pauses a
function.
next()
resumes
where it left
off.
and
that
7.1. DIVING IN
iterators
iterators
can
7.2.1. THE
METHOD
not
,
, and
, begin and
iter(f) calls
f.__iter__
next(f) calls
f.__next__
any
class variable
that
i.e.
and
again
not
Maximum performance.
long
8.1. DIVING IN
cryptarithms
alphametics
The most
well-known
alphametic
puzzle is
This is the
hardest
tongue
twister in
it doesnt return
overlapping matches
the English
language.
e.g.
The
module has
all kinds of
fun stuff.
MODULE
i.e.
longest
really
byte
Now youre
getting to
the really
fun part.
any
any
eval() is
EVIL
NOT
I cant imagine
some
WILL
any
i.e.
will
is
unique
K
paper
hand
punchcards
liked it!
test-driven development
sir
Every test is
an island.
anything
does
right
Write a test
that fails,
then code
until it
passes.
Now
The
Pythonic
way to halt
and catch
fire is to
baaaaaaad
raise an
exception.
three
thats
not
and
10.1. DIVING IN
Now
seem
all
10.3. REFACTORING
prove
10.4. SUMMARY
before
11.1. DIVING IN
M
11.2. READING FROM TEXT FILES
relative path
The default
encoding is
platformdependent.
platform-dependent
stream object
might
Always
specify an
parameter
when you
open a file.
bytes
characters
that
not
is good.
is
better.
Python calls
automatically
the
stream object is also an iterator
Just open a
file and
start
writing.
never
strings
bytes
the
number of bytes to read
To read
from a fake
file, just call
.
youre not
limiting yourself to real files
,
,
.
two
i.e.
i.e.
12.1. DIVING IN
document
elements
start tag
end tag
closed
nested
subelement
child
root element
not an
XML
document
attributes
Attribute names
Attribute values
text content
empty
modules
namespaces
default namespace
prefix
identical
&
root element
not
local name
direct
direct children
i.e.
is
descendant
i.e.
any
is
nodes
default namespace
not
no guarantee of interoperability
13.1. DIVING IN
serializes
perfect replica
is
Notes
JSON
Python 3
&
thats
returns a Python
dictionary
list
14.1. DIVING IN
14.2.1. CACHING
latency
means
don't bug
me until
next week.
A year!
without
generating any network activity whatsoever
dont
its
might
means
same shit,
different
day.
hasnt
really
only
means
theres
nothing new
under the
sun.
14.2.4. COMPRESSION
14.2.5. REDIRECTS
means look
over there!
temporary redirect
permanent redirect
e.g.
i.e.
Were
downloading
3070 bytes
when we
could have
just
downloaded
941.
isnt
14.5. INTRODUCING
STRINGS
i.e.
both
14.5.2. HOW
HANDLES CACHING
Whats on
the wire?
Absolutely
nothing.
automatically
by default
all
in
both directions
not
next
14.5.3. HOW
HANDLES
AND
HEADERS
freshness indicators
does not generate a single
byte of network activity
might
validators
and no data
and no data
two
last time
14.5.4. HOW
HANDLES COMPRESSION
We have
both kinds
of music,
country
AND
western.
14.5.5. HOW
HANDLES REDIRECTS
final
didnt
didnt
URL
Identi.ca
REST API
Method: statuses/update
URL
Formats
HTTP
Method(s)
Requires Authentication
Parameters
a.k.a.
payload
requires
U R L -encoded
i.e.
payload
payload
please
TO
15.1. DIVING IN
MODULE
Encoding
detection is
really
language
detection in
drag.
15.3.1. U T F - N WITH A B O M
Seriously,
wheres my
Unicode
pony?
15.3.5.
15.4. RUNNING
can
was
file in a directory, it assumes that all of the files in that directory are part of the same
relative import
only
CANT
IS INVALID SYNTAX
You do have
tests, right?
15.6.3. NAME
IS NOT DEFINED
open() is the
new file().
PapayaWhip
is the new
black.
bytes
Not an
array of
characters,
but an
array of
bytes.
OBJECT TO
IMPLICITLY
crash
two
AND
second time
Each item in
a string is a
string. Each
item in a
byte array
is an
integer.
15.6.7.
1, BUT
FOUND
>=
IS NOT DEFINED
15.7. SUMMARY
You
only
16.1. DIVING IN
R
setup script
Choose a license
you
name
version
author
author_email
url
description
long_description
classifiers
not
Dont do that.
always
Programming Language
License
Operating System
Development Status
Intended Audience
Framework
Topic
manifest file
not
NAME
17.1. DIVING IN
S
can
17.2.
Notes
STATEMENT
Python 2
Python 3
Notes
17.4.
Python 2
Python 3
GLOBAL FUNCTION
Notes
17.5.
Notes
Python 2
Python 3
DATA TYPE
Python 2
Python 3
Notes
Python 2
Python 3
17.7.
Notes
DICTIONARY METHOD
Python 2
Python 3
do
Notes
Python 2
Python 3
no change
17.9.1.
Notes
Python 2
Python 3
17.9.2.
Notes
Python 2
Python 3
Notes
Python 2
Python 3
17.9.3.
Notes
Python 2
Python 3
17.9.4.
Notes
Python 2
Python 3
Python 2
Python 3
Notes
Python 2
17.11.
Notes
Python 3
ITERATOR METHOD
Python 2
Python 3
no change
17.12.
Notes
GLOBAL FUNCTION
Python 2
Python 3
no change
no change
no change
already
17.13.
Notes
GLOBAL FUNCTION
Python 2
Python 3
no change
no change
17.14.
Notes
GLOBAL FUNCTION
Python 2
17.15.
Notes
Python 3
GLOBAL FUNCTION
Python 2
Python 3
17.16.
Notes
17.17.
Notes
17.18.
GLOBAL FUNCTION
Python 2
Python 3
STATEMENT
Python 2
Python 3
STATEMENT
Notes
17.19.
Notes
Python 2
Python 3
LITERALS (BACKTICKS)
Python 2
Python 3
&
17.20.
Notes
STATEMENT
Python 2
Python 3
no change
no change
all
all
17.21.
Notes
STATEMENT
Python 2
Python 3
unchanged
unsupported
17.22.
Notes
METHOD ON GENERATORS
Python 2
Python 3
no change
unsupported
with
only
17.23.
Notes
GLOBAL FUNCTION
Python 2
Python 3
no change
no change
not
17.24.
Notes
AND
Python 2
GLOBAL FUNCTIONS
Python 3
17.25.
Notes
FUNCTION ATTRIBUTES
Python 2
Python 3
docstring
17.26.
Notes
I/O METHOD
Python 2
Python 3
no change (broken)
17.27.
PARAMETERS
Notes
Python 2
Python 3
unchanged
Notes
17.29.
Python 2
Python 3
SPECIAL METHOD
Notes
Python 2
Python 3
no change
Notes
Python 2
Python 3
Python 2
Python 3
17.31.
Notes
17.32.
Notes
17.33.
Notes
GLOBAL FUNCTION
Python 2
Python 3
GLOBAL FUNCTION
Python 2
Python 3
no change
17.34.
Notes
EXCEPTION
Python 2
Python 3
17.35.
Notes
MODULE CONSTANTS
Python 2
Python 3
17.36.
Notes
GLOBAL FUNCTION
Python 2
17.37.
Notes
DATATYPE
Python 2
17.38.
Notes
Python 3
Python 3
MODULE
Python 2
Python 3
17.39.
Notes
Python 2
Python 3
Notes
Python 2
Python 3
17.41.
Notes
FUNCTION
Python 2
Python 3
17.42. METACLASSES
Notes
Python 2
Python 3
unchanged
17.43.1.
Notes
17.43.2.
LITERALS (EXPLICIT)
Before
After
Notes
Before
After
Notes
Before
After
Notes
Before
After
18.1. DIVING IN
T
18.2. BASICS
Notes
You Want
So You Write
after
New in Python 3
Notes
You Want
So You Write
And Python
Calls
You Want
So You Write
all
is still called
is called unconditionally
Notes
You Want
So You Write
Notes
You Want
So You Write
And Python
Calls
Notes
You Want
So You Write
Notes
You Want
So You Write
And Python
Calls
&
not
reflected operands
e.g.
Notes
You Want
So You Write
And Python
Calls
&
Notes
You Want
So You Write
And Python
Calls
Notes
You Want
So You Write
Notes
You Want
So You Write
And Python
Calls
Notes
You Want
So You Write
Notes
You Want
So You Write
BLOCK
Notes
You Want
So You Write
&
key?
variously attributed
20.1. DIVING IN
F
20.2. GETTING TO THE COMMAND LINE