FOURTH EDITION
Learning Python
Learning Python, Fourth Edition
Editor:
Production Editor:
Copyeditor:
Production Services:
Indexer:
Cover Designer:
Interior Designer:
Illustrator:
Printing History:
www.it-ebooks.info
Table of Contents
Preface . . . xxxi
Part I. Getting Started
1. A Python Q&A Session . . . 3
2. How Python Runs Programs . . . 23
3. How You Run Programs . . . 35
viii | Table of Contents
Part II. Types and Operations
4. Introducing Python Object Types . . . 75
5. Numeric Types . . . 105
6. The Dynamic Typing Interlude . . . 143
x | Table of Contents
7. Strings . . . 155
8. Lists and Dictionaries . . . 197
9. Tuples, Files, and Everything Else . . . 225
Part III. Statements and Syntax
10. Introducing Python Statements . . . 261
xii | Table of Contents
11. Assignments, Expressions, and Prints . . . 279
12. if Tests and Syntax Rules . . . 311
13. while and for Loops . . . 327
14. Iterations and Comprehensions, Part 1 . . . 351
xiv | Table of Contents
15. The Documentation Interlude . . . 375
Part IV. Functions
16. Function Basics . . . 395
17. Scopes . . . 407
18. Arguments . . . 435
xvi | Table of Contents
19. Advanced Function Topics . . . 463
20. Iterations and Comprehensions, Part 2 . . . 485
Part V. Modules
21. Modules: The Big Picture . . . 529
22. Module Coding Basics . . . 543
xviii | Table of Contents
23. Module Packages . . . 561
24. Advanced Module Topics . . . 583
Part VI. Classes and OOP
25. OOP: The Big Picture . . . 611
26. Class Coding Basics . . . 625
xx | Table of Contents
27. A More Realistic Example . . . 643
28. Class Coding Details . . . 681
29. Operator Overloading . . . 705
xxii | Table of Contents
30. Designing with Classes . . . 737
31. Advanced Class Topics . . . 773
Part VII. Exceptions and Tools
32. Exception Basics . . . 825
33. Exception Coding Details . . . 835
xxiv | Table of Contents
34. Exception Objects . . . 857
35. Designing with Exceptions . . . 873
Part VIII. Advanced Topics
36. Unicode and Byte Strings . . . 895
xxvi | Table of Contents
37. Managed Attributes . . . 941
38. Decorators . . . 983
xxviii | Table of Contents
39. Metaclasses . . . 1051
Part IX. Appendixes
A. Installation and Configuration . . . 1089
B. Solutions to End-of-Part Exercises . . . 1101
Index . . . 1139
Preface
About This Fourth Edition
Coverage for Both 3.0 and 2.6
nonlocal
format
xxxii | Preface
format with
New Chapters
Changes to Existing Material
map
zip
__contains__ __bool__
__index__
format
exec
xxxiv | Preface
Specific Language Extensions in 2.6 and 3.0
list
Extension
Covered in chapter(s)
The
printfunction in 3.0
11
The
nonlocal x,ystatement in 3.0
17
The
str.formatmethod in 2.6 and 3.0
7
String types in 3.0:
strfor Unicode text,
bytesfor binary data
7
,
36
Text and binary file distinctions in 3.0
9
,
36
Class decorators in 2.6 and 3.0:
@private('age')31
,
38
New iterators in 3.0:
range,
map,
zip14
,
20
Dictionary views in 3.0:
D.keys,
D.values,
D.items8
,
14
Division operators in 3.0: remainders,
/and
//5
Set literals in 3.0:
{a, b, c}5
Set comprehensions in 3.0:
{x**2 for x in seq}4
,
5
,
14
,
20
Dictionary comprehensions in 3.0:
{x: x**2 for x in seq}4
,
8
,
14
,
20
Binary digit-string support in 2.6 and 3.0:
0b0101,bin(I)5
The fraction number type in 2.6 and 3.0:
Fraction(1, 3)5
Function annotations in 3.0:
def f(a:99, b:str)->int19
Keyword-only arguments in 3.0:
def f(a, *b, c, **d)18
,
20
Extended sequence unpacking in 3.0:
a, *b = seq11
,
13
Relative import syntax for packages enabled in 3.0:
from .23
Context managers enabled in 2.6 and 3.0:
with/
as33
,
35
Exception syntax changes in 3.0:
raise,
except/
as, superclass
33
,
34
Extension
Covered in chapter(s)
Exception chaining in 3.0:
raise e2 from e133
Reserved word changes in 2.6 and 3.0
11
New-style class cutover in 3.0
31
Property decorators in 2.6 and 3.0:
@property37
Descriptor use in 2.6 and 3.0
31
,
38
Metaclass use in 2.6 and 3.0
31
,
39
Abstract base classes support in 2.6 and 3.0
28
Specific Language Removals in 3.0
Removed
Replacement
Covered in chapter(s)
reload(M) imp.reload(M)(or
exec)
3
,
22
apply(f, ps, ks) f(*ps, **ks)
18
`X` repr(X)
5
X <> Y X != Y
5
long int
5
9999L 9999
5
D.has_key(K) K in D
(or
D.get(key) != None)
8
raw_input input
3
,
10
old
input eval(input())3
xrange range
14
file open
(and
iomodule classes)
9
X.next X.__next__
, called by
next(X)14
,
20
,
29
X.__getslice__ X.__getitem__
passed a
sliceobject
7
,
29
X.__setslice__ X.__setitem__
passed a
sliceobject
7
,
29
reduce functools.reduce
(or loop code)
14
,
19
execfile(filename) exec(open(filename).read())
3
exec open(filename) exec(open(filename).read())
3
0777 0o777
5
print x, y print(x, y)
11
xxxvi | Preface
Removed
Replacement
Covered in chapter(s)
print >> F, x, y print(x, y, file=F)11
print x, y, print(x, y, end=' ')
11
u'ccc' 'ccc'
7
,
36
'bbb'
for byte strings
b'bbb'7
,
9
,
36
raise E, V raise E(V)
32
,
33
,
34
except E, X: except E as X:
32
,
33
,
34
def f((a, b)): def f(x): (a, b) = x
11
,
18
,
20
file.xreadlines for line in file:
(or
X=iter(file))
13
,
14
D.keys()
, etc. as lists
list(D.keys())(dictionary views)
8
,
14
map()
,
range(), etc. as lists
list(map()),
list(range())(built-ins)
14
map(None, ...) zip
(or manual code to pad results)
13
,
20
X=D.keys(); X.sort() sorted(D)
(or
list(D.keys()))
4
,
8
,
14
cmp(x, y) (x > y) - (x < y)
29
X.__cmp__(y) __lt__
,
__gt__,
__eq__, etc.
29
X.__nonzero__ X.__bool__
29
X.__hex__, X.__oct__ X._index__
29
Sort comparison functions
Use
key=transformor
reverse=True8
Dictionary
<,
>,
<=,
>=Compare
sorted(D.items())(or loop code)
8
,
9
types.ListType list
(
typesis for nonbuilt-in names only)
9
__metaclass__ = M class C(metaclass=M):
28
,
31
,
39
__builtin__ builtins
(renamed)
17
Tkinter tkinter
(renamed)
18
,
19
,
24
,
29
,
30
sys.exc_type
,
exc_value sys.exc_info()[0],
[1]34
,
35
function.func_code function.__code__
19
,
38
__getattr__
run by built-ins
Redefine
__X__methods in wrapper classes
30
,
37
,
38
-t
,
–ttcommand-line switches
Inconsistent tabs/spaces use is always an error
10
,
12
from ... *
, within a function
May only appear at the top level of a file
22
import mod
, in same package
from . import mod, package-relative form
23
class MyException: class MyException(Exception):
34
exceptions
module
Built-in scope, library manual
34
thread
,
Queuemodules
_thread,
queue(both renamed)
17
anydbm
module
dbm(renamed)
27
cPickle
module
_pickle(renamed, used automatically)
9
os.popen2/3/4 subprocess.Popen
(
os.popenretained)
14
String-based exceptions
Class-based exceptions (also required in 2.6)
32
,
33
,
34
Removed
Replacement
Covered in chapter(s)
String module functions
String object methods
7
Unbound methods
Functions (
staticmethodto call via instance)
30
,
31
Mixed type comparisons, sorts
Nonnumeric mixed type comparisons are errors
5
,
9
About The Third Edition
The Third Edition’s Python Language Changes
xxxviii | Preface
B if A else C
with as
try except finally
sorted sum any all enumerate
distutils unittest
doctest
True
False
sys.exc_info
apply
reduce
apply
The Third Edition’s Python Training Changes
for
The Third Edition’s Structural Changes
The Third Edition’s Scope Changes
xl | Preface
About This Book
This Book’s Prerequisites
This Book’s Scope and Other Books
xlii | Preface
This Book’s Style and Structure
__name__
xliv | Preface
Book Updates
^H^H^H
About the Programs in This Book
Using Code Examples
Font Conventions
Constant width
Constant width bold
Constant width italic
<Constant width>
xlvi | Preface
% C:\Python30> % >>> ... #
Safari® Books Online
How to Contact Us
Acknowledgments
xlviii | Preface
PART I
CHAPTER 1
A Python Q&A Session
Why Do People Use Python?
Software Quality
4 | Chapter 1: A Python Q&A Session
Developer Productivity
Is Python a “Scripting Language”?
import this
6 | Chapter 1: A Python Q&A Session
OK, but What’s the Downside?
Who Uses Python Today?
8 | Chapter 1: A Python Q&A Session
What Can I Do with Python?
Systems Programming
GUIs
Internet Scripting
Component Integration
10 | Chapter 1: A Python Q&A Session
Database Programming
pickle
Rapid Prototyping
Numeric and Scientific Programming
Gaming, Images, Serial Ports, XML, Robots, and More
xml
xmlrpclib
How Is Python Supported?
12 | Chapter 1: A Python Q&A Session
What Are Python’s Technical Strengths?
It’s Object-Oriented
It’s Free
It’s Portable
14 | Chapter 1: A Python Q&A Session
It’s Powerful
It’s Mixable
It’s Easy to Use
16 | Chapter 1: A Python Q&A Session
It’s Easy to Learn
It’s Named After Monty Python
How Does Python Stack Up to Language X?
Chapter Summary
18 | Chapter 1: A Python Q&A Session
Test Your Knowledge: Quiz
import this
Test Your Knowledge: Answers
import this
Python Is Engineering, Not Art
20 | Chapter 1: A Python Q&A Session
CHAPTER 2
How Python Runs Programs
Introducing the Python Interpreter
Program Execution
The Programmer’s View
print('hello world') print(2 ** 100)
24 | Chapter 2: How Python Runs Programs
hello world
1267650600228229401496703205376
C:\temp> python script0.py hello world
1267650600228229401496703205376
Python’s View
Byte code compilation
26 | Chapter 2: How Python Runs Programs
The Python Virtual Machine (PVM)
Performance implications
Development implications
eval
exec
28 | Chapter 2: How Python Runs Programs
Execution Model Variations
Python Implementation Alternatives
CPython
Jython
IronPython
Execution Optimization Tools
The Psyco just-in-time compiler
30 | Chapter 2: How Python Runs Programs
The Shedskin C++ translator
Frozen Binaries
32 | Chapter 2: How Python Runs Programs
Other Execution Options
Future Possibilities?
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
34 | Chapter 2: How Python Runs Programs
www.it-ebooks.info
1.#code#that#executes#other#code
2.#what#you#actually#write#for#the#program
3.#Is#what#python#compiles
4.#Python#Virtual#Machine,#the#engine#
that#interprets#
the#byte#code.#
5.#IronPython,#JPython#
CHAPTER 3
How You Run Programs
exec
The Interactive Prompt
python
% python
Python 3.0.1 (r301:69561, Feb 13 2009, 20:04:18) [MSC v.1500 32 bit (Intel)] ... Type "help", "copyright", "credits" or "license" for more information.
>>>
python
PATH
/usr/local/bin/python
/usr/bin/python
C:\Python30\python
C:\misc> c:\python30\pythonPython 3.0.1 (r301:69561, Feb 13 2009, 20:04:18) [MSC v.1500 32 bit (Intel)] ... Type "help", "copyright", "credits" or "license" for more information.
>>>
cd c:\python30
C:\misc> cd C:\Python30 C:\Python30> python
Python 3.0.1 (r301:69561, Feb 13 2009, 20:04:18) [MSC v.1500 32 bit (Intel)] ... Type "help", "copyright", "credits" or "license" for more information.
>>>
python
36 | Chapter 3: How You Run Programs
Running Code Interactively
>>>
>>>
>>>
2 ** 8
>>> lumberjack = 'okay' >>> lumberjack 'okay' >>> 2 ** 8 256 >>> %lumberjack
2 ** 8
>>>
Why the Interactive Prompt?
Experimenting
'Spam!' * 8
>>> 'Spam!' * 8 'Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!'
*
38 | Chapter 3: How You Run Programs
>>> X Traceback (most recent call last):
File "<stdin>", line 1, in <module> NameError: name 'X' is not defined
Testing
>>> import os
>>> os.getcwd() 'c:\\Python30'
Using the Interactive Prompt
os.system
...
>>>
...
>>>
...
sys
40 | Chapter 3: How You Run Programs
Entering multiline statements
for
if
>>> for x in 'spam': ... print(x) ... >>> for x in 'spam': ... print(x) ... print('done')File "<stdin>", line 3 print('done') ^
SyntaxError: invalid syntax
System Command Lines and Files
python
A First Script
import sys print(sys.platform) print(2 ** 100) x = 'Spam!' print(x * 8)x
sys.platform
sys
42 | Chapter 3: How You Run Programs
#
#
Running Files with Command Lines
python
% python script1.py win32 1267650600228229401496703205376 Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!PATH
% python script1.py > saveit.txt
C:\Python30> python script1.py win32
1267650600228229401496703205376
Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!
PATH
D:\temp> C:\python30\python script1.py win32
1267650600228229401496703205376
Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!
D:\temp> script1.py
D:\other> python c:\code\otherscript.py
PATH
D:\other> C:\Python30\python c:\code\otherscript.py
Using Command Lines and Files
44 | Chapter 3: How You Run Programs
python script1.py
python script1
import
import script1
python d:\tests\spam.py
import spam
Unix Executable Scripts (#!)
#!
chmod +x file.py
#!/usr/local/bin/python
print('The Bright Side ' + 'of Life...')
#
chmod +x brian
% brian
The Bright Side of Life...
python
brian.py python brian.py
#!
#!
46 | Chapter 3: How You Run Programs
C:\misc> python brian The Bright Side of Life...
#!
The Unix env Lookup Trick
#!/usr/bin/env python
...script goes here...
PATH
PATH
Clicking File Icons
#!
Clicking Icons on Windows
import sys print(sys.platform)
print(2 ** 100) x = 'Spam!'
print(x * 8)
C:\misc> c:\python30\python script1.py win32
1267650600228229401496703205376
48 | Chapter 3: How You Run Programs
The input Trick
input
raw_input
import sys print(sys.platform) print(2 ** 100) x = 'Spam!' print(x * 8) input()input
input
input
input('Press
Enter to exit')
nextinput = input()
python spam.py
< input.txt
input
raw_input() input() input input raw_input input eval(input())Other Icon-Click Limitations
input
input
50 | Chapter 3: How You Run Programs
try
Module Imports and Reloads
import
input
C:\misc> c:\python30\python >>> import script1 win32 1267650600228229401496703205376 Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!>>> import script1 >>> import script1
reload
imp
>>> from imp import reload >>> reload(script1)
win32 65536
Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam! <module 'script1' from 'script1.py'> >>>
from
reload
2 ** 16
import
reload
reload
reload
import
reload
import
reload
reload
52 | Chapter 3: How You Run Programs
reload imp
import imp imp.reload(M) from imp import reload reload(M)
import from reload reload reload reload from from reload import reload import module.attribute
The Grander Module Story: Attributes
import
from
reload
title = "The Meaning of Life"
title
title
import
% python >>> import myfile >>> print(myfile.title) The Meaning of Life
object.attribute
title
myfile
myfile.title
from
% python >>> from myfile import title >>> print(title) The Meaning of Life
from
import
from
title
myfile.title
import
from
title
a = 'dead' b = 'parrot' c = 'sketch' print(a, b, c)54 | Chapter 3: How You Run Programs
% python threenames.py dead parrot sketch
import
from
import
from
% python
>>> import threenames dead parrot sketch
>>>
>>> threenames.b, threenames.c ('parrot', 'sketch')
>>>
>>> from threenames import a, b, c >>> b, c
('parrot', 'sketch')
dir
>>> dir(threenames)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'a', 'b', 'c']
dir
a b
c
dir
Modules and namespaces
import
from from
import from
from
from
import and reload Usage Notes
import
reload
reload
reload
reload
from
reload
56 | Chapter 3: How You Run Programs
Using exec to Run Module Files
exec(open('module.py').read())
exec
C:\misc> c:\python30\python >>> exec(open('script1.py').read()) win32 65536 Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!...change script1.py in a text edit window...
>>> exec(open('script1.py').read()) win32 4294967296 Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!
exec
exec
exec
exec
exec
from
x
exec
>>> x = 999 >>> exec(open('script1.py').read()) ...same outout... >>> x 'Spam!' sys.path sys PYTHONPATH PYTHONPATHimport
execfile('module.py') exec(open('module.py')) exec(open('module.py').read()) exec execThe IDLE User Interface
exec
IDLE Basics
58 | Chapter 3: How You Run Programs
>>>
yum tkinter
idle
Using IDLE
60 | Chapter 3: How You Run Programs
-n
idle.py -n
cd
import
Advanced IDLE Tools
62 | Chapter 3: How You Run Programs
Other IDEs
Other Launch Options
Embedding Calls
64 | Chapter 3: How You Run Programs
#include <Python.h> ...
Py_Initialize(); PyRun_SimpleString("x = 'brave ' + 'sir robin'");
Frozen Binary Executables
Text Editor Launch Options
Still Other Launch Options
os.popen os.system
Future Possibilities?
Which Option Should I Use?
66 | Chapter 3: How You Run Programs
Debugging Python Code
print print print # print printChapter Summary
exec
Test Your Knowledge: Quiz
68 | Chapter 3: How You Run Programs
www.it-ebooks.info
unix%open%terminal,%use%IDE3GUI
in%the%directory%you%have%python
icon%clicking,%within%the%text%file%you
are%using,%by%using%IDE,%command
modules%run%only%once,%even%if%
changes%are%made%
open/new%file%then%run3>run%module
some%tools%are%not%available
a%module%is%a%namespace,%a%package
of%variables.%
%type%python
Test Your Knowledge: Answers
python
PATH
cd
python
C:\Python30\python
exec
#!
input
Test Your Knowledge: Part I Exercises
>>>
"Hello
World!"
cd
PATH
PATH
print('Hello module world!')
python module1.py
exec
>>>
70 | Chapter 3: How You Run Programs
#!
#!
2 ** 500
1 / 0
L = [1, 2] L.append(L) Lhelp
72 | Chapter 3: How You Run Programs
PART II
CHAPTER 4
Introducing Python Object Types
Why Use Built-in Types?
76 | Chapter 4: Introducing Python Object Types
Python’s Core Data Types
Object type
Example literals/creation
Numbers
1234,
3.1415,
3+4j,
Decimal,
FractionStrings
'spam',
"guido's",
b'a\x01c'Lists
[1, [2, 'three'], 4]Dictionaries
{'food': 'spam', 'taste': 'yum'}Tuples
(1, 'spam', 4, 'U')Files
myfile = open('eggs', 'r')Sets
set('abc'), {'a', 'b', 'c'}Other core types
Booleans, types,
NoneProgram unit types
Functions, modules, classes (
Part IV
,
Part V
,
Part VI
)
Implementation-related types
Compiled code, stack tracebacks (
Part IV
,
Part VII
)
def class import
lambda
>>> 'spam'
const
Numbers
+
*
**
78 | Chapter 4: Introducing Python Object Types
>>> 123 + 222 345 >>> 1.5 * 4 6.0 >>> 2 ** 100 1267650600228229401496703205376 >>> len(str(2 ** 1000000)) 301030 >>> 3.1415 * 2 6.2830000000000004 >>> print(3.1415 * 2) 6.283
repr
str
math
random
>>> import random >>> random.random() 0.59268735266273953 >>> random.choice([1, 2, 3, 4]) 1Numbers | 79
Strings
Sequence Operations
len
>>> S = 'Spam' >>> len(S) 4 >>> S[0] 'S' >>> S[1] 'p'S
>>> S[-1] 'm' >>> S[-2] 'a'80 | Chapter 4: Introducing Python Object Types
>>> S[-1] 'm' >>> S[len(S)-1] 'm' >>> S 'Spam' >>> S[1:3] 'pa'
X[I:J]
X
I
J
S
>>> S[1:] 'pam' >>> S 'Spam' >>> S[0:3] 'Spa' >>> S[:3] 'Spa' >>> S[:-1] 'Spa' >>> S[:] 'Spam' >>> S Spam' >>> S + 'xyz'Strings | 81
'Spamxyz' >>> S 'Spam' >>> S * 8 'SpamSpamSpamSpamSpamSpamSpamSpam'
+
+
Immutability
>>> S 'Spam' >>> S[0] = 'z'...error text omitted...
TypeError: 'str' object does not support item assignment >>> S = 'z' + S[1:]
>>> S 'zpam'
Type-Specific Methods
82 | Chapter 4: Introducing Python Object Types
find
−1
replace
>>> S.find('pa') 1 >>> S 'Spam' >>> S.replace('pa', 'XYZ') 'SXYZm' >>> S 'Spam' >>> line = 'aaa,bbb,ccccc,dd' >>> line.split(',') ['aaa', 'bbb', 'ccccc', 'dd'] >>> S = 'spam' >>> S.upper() 'SPAM' >>> S.isalpha() True >>> line = 'aaa,bbb,ccccc,dd\n' >>> line = line.rstrip() >>> line 'aaa,bbb,ccccc,dd'>>> '%s, eggs, and %s' % ('spam', 'SPAM!') 'spam, eggs, and SPAM!'
>>> '{0}, eggs, and {1}'.format('spam', 'SPAM!') 'spam, eggs, and SPAM!'
len(X) X[0]
aString.upper()
Getting Help
dir
S
>>> dir(S)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum','isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
dir
help
>>> help(S.replace)
Help on built-in function replace: replace(...)
S.replace (old, new[, count]) -> str
Return a copy of S with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
help
help(S)
84 | Chapter 4: Introducing Python Object Types
dir
help
Other Ways to Code Strings
>>> S = 'A\nB\tC' >>> len(S) 5 >>> ord('\n') 10 >>> S = 'A\0B\0C' >>> len(S) 5 >>> msg = """ aaaaaaaaaaaaa bbb'''bbbbbbbbbb""bbbbbbb'bbbb cccccccccccccc""" >>> msg '\naaaaaaaaaaaaa\nbbb\'\'\'bbbbbbbbbb""bbbbbbb\'bbbb\ncccccccccccccc'
r
str
bytes
str
str
bytes
Pattern Matching
Strings | 85
re
>>> import re
>>> match = re.match('Hello[ \t]*(.*)world', 'Hello Python world') >>> match.group(1)
'Python '
>>> match = re.match('/(.*)/(.*)/(.*)', '/usr/home/lumberjack') >>> match.groups()
('usr', 'home', 'lumberjack')
Lists
Sequence Operations
>>> L = [123, 'spam', 1.23] >>> len(L) 3 >>> L[0] 123 >>> L[:-1] [123, 'spam'] >>> L + [4, 5, 6] [123, 'spam', 1.23, 4, 5, 6]86 | Chapter 4: Introducing Python Object Types
>>> L [123, 'spam', 1.23]
Type-Specific Operations
>>> L.append('NI') >>> L [123, 'spam', 1.23, 'NI'] >>> L.pop(2) 1.23 >>> L [123, 'spam', 'NI']append
pop
del
insert
remove
>>> M = ['bb', 'aa', 'cc'] >>> M.sort() >>> M ['aa', 'bb', 'cc'] >>> M.reverse() >>> M ['cc', 'bb', 'aa']sort
reverse
Bounds Checking
>>> L [123, 'spam', 'NI'] >>> L[99]...error text omitted...
IndexError: list index out of range
>>> L[99] = 1
...error text omitted...
IndexError: list assignment index out of range
append
Nesting
>>> M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> M [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> M[1] [4, 5, 6] >>> M[1][2] 6Comprehensions
88 | Chapter 4: Introducing Python Object Types
>>> col2 = [row[1] for row in M] >>> col2 [2, 5, 8] >>> M [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
row
row[1]
M
>>> [row[1] + 1 for row in M] [3, 6, 9]
>>> [row[1] for row in M if row[1] % 2 == 0] [2, 8]
if
%
>>> diag = [M[i][i] for i in [0, 1, 2]] >>> diag
[1, 5, 9]
>>> doubles = [c * 2 for c in 'spam'] >>> doubles
['ss', 'pp', 'aa', 'mm']
map
filter
sum
>>> G = (sum(row) for row in M) >>> next(G) 6 >>> next(G) 15
map
list
>>> list(map(sum, M)) [6, 15, 24]>>> {sum(row) for row in M} {24, 6, 15}
>>> {i : sum(M[i]) for i in range(3)} {0: 6, 1: 15, 2: 24}
>>> [ord(x) for x in 'spaam'] [115, 112, 97, 97, 109]
>>> {ord(x) for x in 'spaam'} {112, 97, 115, 109}
>>> {x: ord(x) for x in 'spaam'} {'a': 97, 'p': 112, 's': 115, 'm': 109}
Dictionaries
Mapping Operations
>>> D = {'food': 'Spam', 'quantity': 4, 'color': 'pink'}
90 | Chapter 4: Introducing Python Object Types
>>> D['food'] ' ' 'Spam'
>>> D['quantity'] += 1 ' ' >>> D
{'food': 'Spam', 'color': 'pink', 'quantity': 5}
>>> D = {}
>>> D['name'] = 'Bob' >>> D['job'] = 'dev' >>> D['age'] = 40 >>> D
{'age': 40, 'job': 'dev', 'name': 'Bob'} >>> print(D['name'])
Bob
Nesting Revisited
>>> rec = {'name': {'first': 'Bob', 'last': 'Smith'}, 'job': ['dev', 'mgr'],
'age': 40.5}
>>> rec['name'] ' ' {'last': 'Smith', 'first': 'Bob'}
>>> rec['name']['last'] 'Smith' >>> rec['job'] ' ' ['dev', 'mgr'] >>> rec['job'][-1] 'mgr' >>> rec['job'].append('janitor') ' >>> rec
{'age': 40.5, 'job': ['dev', 'mgr', 'janitor'], 'name': {'last': 'Smith', 'first': 'Bob'}}
>>> rec = 0
rec
pickle shelve
92 | Chapter 4: Introducing Python Object Types
Sorting Keys: for Loops
>>> D = {'a': 1, 'b': 2, 'c': 3} >>> D {'a': 1, 'c': 3, 'b': 2}keys
sort
for
for
>>> Ks = list(D.keys()) >>> Ks ['a', 'c', 'b'] >>> Ks.sort() >>> Ks ['a', 'b', 'c'] >>> for key in Ks: print(key, '=>', D[key]) a => 1 b => 2 c => 3sorted
sorted
>>> D {'a': 1, 'c': 3, 'b': 2} >>> for key in sorted(D): print(key, '=>', D[key]) a => 1 b => 2 c => 3for
for
Dictionaries | 93
key
for
while
for
>>> for c in 'spam': print(c.upper()) S P A Mwhile
>>> x = 4 >>> while x > 0: print('spam!' * x) x -= 1 spam!spam!spam!spam! spam!spam!spam! spam!spam! spam!Iteration and Optimization
for
iter
next
sorted
keys
next
94 | Chapter 4: Introducing Python Object Types
>>> squares = [x ** 2 for x in [1, 2, 3, 4, 5]] >>> squares [1, 4, 9, 16, 25]
for
>>> squares = [] >>> for x in [1, 2, 3, 4, 5]: squares.append(x ** 2) >>> squares [1, 4, 9, 16, 25]map
filter
for
time
timeit
profile
Missing Keys: if Tests
>>> D {'a': 1, 'c': 3, 'b': 2} >>> D['e'] = 99 >>> D {'a': 1, 'c': 3, 'b': 2, 'e': 99} >>> D['f']
...error text omitted...
KeyError: 'f'
in
if
for
if
>>> 'f' in D False >>> if not 'f' in D: print('missing') missingif
if
if
else
elif
get
has_key
try
if else
if
>>> value = D.get('x', 0) >>> value 0 >>> value = D['x'] if 'x' in D else 0 >>> value 0Tuples
>>> T = (1, 2, 3, 4) >>> len(T) 4 >> T + (5, 6) (1, 2, 3, 4, 5, 6) >>> T[0] 196 | Chapter 4: Introducing Python Object Types
>>> T.index(4) 3
>>> T.count(4) 1
>>> T[0] = 2
...error text omitted...
TypeError: 'tuple' object does not support item assignment
>>> T = ('spam', 3.0, [11, 22, 33]) >>> T[1] 3.0 >>> T[2][1] 22 >>> T.append(4)
AttributeError: 'tuple' object has no attribute 'append'
Why Tuples?
Files
open
'w'
>>> f = open('data.txt', 'w') >>> f.write('Hello\n') 6 >>> f.write('world\n') 6 >>> f.close()Files | 97
'r'
>>> f = open('data.txt') >>> text = f.read() >>> text 'Hello\nworld\n' >>> print(text) Hello world >>> text.split() ['Hello', 'world']read
readline
seek
for
dir
help
>>> dir(f)[ ...many names omitted...
'buffer', 'close', 'closed', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'line_buffering', 'mode', 'name', 'newlines', 'read', 'readable', 'readline', 'readlines', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 'writelines']
>>>help(f.seek)
...try it and see...
bytes
>>> data = open('data.bin', 'rb').read() >>> data b'\x00\x00\x00\x07spam\x00\x08'
>>> data[4:8] b'spam'
98 | Chapter 4: Introducing Python Object Types
Other File-Like Tools
open
Other Core Types
set
{...}
>>> X = set('spam') >>> Y = {'h', 'a', 'm'} >>> X, Y ({'a', 'p', 's', 'm'}, {'a', 'h', 'm'}) >>> X & Y {'a', 'm'} >>> X | Y {'a', 'p', 's', 'h', 'm'} >>> X – Y {'p', 's'} >>> {x ** 2 for x in [1, 2, 3, 4]} {16, 1, 4, 9} >>> 1 / 3 0.33333333333333331 >>> (2/3) + (1/2)1.1666666666666665 >>> import decimal >>> d = decimal.Decimal('3.141') >>> d + 1 Decimal('4.141') >>> decimal.getcontext().prec = 2 >>> decimal.Decimal('1.00') / decimal.Decimal('3.00') Decimal('0.33')
>>> from fractions import Fraction >>> f = Fraction(2, 3) >>> f + 1 Fraction(5, 3) >>> f + Fraction(1, 2) Fraction(7, 6)
True
False
None
>>> 1 > 2, 1 < 2 (False, True) >>> bool('spam') True >>> X = None >>> print(X) None >>> L = [None] * 100 >>> L[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, ...a list of 100 Nones...]
How to Break Your Code’s Flexibility
type
L
>>> type(L) <type 'list'> >>> type(type(L)) <type 'type'> >>> type(L) <class 'list'>100 | Chapter 4: Introducing Python Object Types
>>> type(type(L)) <class 'type'> >>> if type(L) == type([]): print('yes') yes >>> if type(L) == list: print('yes') yes >>> if isinstance(L, list): print('yes') yes
User-Defined Classes
>>> class Worker:def __init__(self, name, pay): self.name = name
self.pay = pay def lastName(self):
return self.name.split()[-1] def giveRaise(self, percent):
self.pay *= (1.0 + percent)
name
pay
self
>>> bob = Worker('Bob Smith', 50000) >>> sue = Worker('Sue Jones', 60000) >>> bob.lastName() 'Smith' >>> sue.lastName() 'Jones' >>> sue.giveRaise(.10) >>> sue.pay 66000.0
Worker
name
pay
And Everything Else
102 | Chapter 4: Introducing Python Object Types
class
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
None
'spam'
open
+
104 | Chapter 4: Introducing Python Object Types
CHAPTER 5
Numeric Types
Numeric Type Basics
Numeric Literals
Literal
Interpretation
1234
,
−24,
0,
99999999999999Integers (unlimited size)
1.23
,
1.,
3.14e-10,
4E210,
4.0e+210Floating-point numbers
0177
,
0x9ff,
0b101010Octal, hex, and binary literals in 2.6
0o177
,
0x9ff,
0b101010Octal, hex, and binary literals in 3.0
3+4j
,
3.0+4.0j,
3JComplex number literals
e
E
106 | Chapter 5: Numeric Types
l
L
L
l
L
0x
0X
0 9
A F
0o
0O
0 7
0
0o
0b
0B
0 1
hex(I) oct(I)
bin(I)
int(str, base)
realpart+imaginarypart
imaginarypart
j
J
realpart
imaginarypart
complex(real, imag)
Built-in Numeric Tools
+ - * / >> ** &
pow abs round int hex bin
random math
as_integer_ratio
is_integer
bit_length
Python Expression Operators
X
Y
X + Y
+
X
Y
X
Y
+ − * /
%
<<
&
is
lambda
108 | Chapter 5: Numeric Types
Operators
Description
yield x
Generator function
sendprotocol
lambda args: expression
Anonymous function generation
x if y else z
Ternary selection (
xis evaluated only if
yis true)
x or y
Logical OR (
yis evaluated only if
xis false)
x and y
Logical AND (
yis evaluated only if
xis true)
not x
Logical negation
x in y
,
x not in yMembership (iterables, sets)
x is y
,
x is not yObject identity tests
x < y
,
x <= y,
x > y,
x >= y x == y,
x != yMagnitude comparison, set subset and superset;
Value equality operators
x | y
Bitwise OR, set union
x ^ y
Bitwise XOR, set symmetric difference
x & y
Bitwise AND, set intersection
x << y
,
x >> yShift
xleft or right by
ybits
x + y x – y
Addition, concatenation;
Subtraction, set difference
x * y x % y x / y
,
x // yMultiplication, repetition;
Remainder, format;
Division: true and floor
−x
,
+xNegation, identity
˜x
Bitwise NOT (inversion)
x ** y
Power (exponentiation)
x[i]
Indexing (sequence, mapping, others)
x[i:j:k]
Slicing
x(...)
Call (function, method, class, other callable)
x.attr
Attribute reference
(...)
Tuple, expression, generator expression
[...]
List, list comprehension
{...}
Dictionary, set, set and dictionary comprehensions
X != Y
X <> Y
X != Y
`X`
repr(X)
str
repr
X // Y
X / Y
[...]
(...)
{...}
yield
if else
send(...)
if
yield
X < Y < Z
X < Y and Y < X
X[I:J:K]
X[slice(I, J, K)]
sorted(dict.items())
110 | Chapter 5: Numeric Types
Mixed operators follow operator precedence
A * B + C * DX + Y * Z
(Y * Z)
X
*
+
A * B
C * D
Parentheses group subexpressions
X + Y * Z
(X + Y) * Z X + (Y * Z)
+
X
Y
*
Mixed types are converted up
40 + 3.14
>>> int(3.1415) 3
>>> float(3) 3.0
Preview: Operator overloading and polymorphism
+
[i]
112 | Chapter 5: Numeric Types
+
+
Numbers in Action
Variables and Basic Expressions
a
b
a
b
% python >>> a = 3 >>> b = 4#
#Numbers in Action | 113
a
b
3
4
>>> a + 1, a – 1 (4, 2) >>> b * 3, b / 2 (12, 2.0) >>> a % 2, b ** 2 (1, 16) >>> 2 + 4.0, 2.0 ** b (6.0, 16.0)a
b
>>> c * 2Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'c' is not defined
>>> b / 2 + a 5.0 >>> print(b / (2.0 + a)) 0.8
/
+
b // 2 + a
+
/
2.0
114 | Chapter 5: Numeric Types
a
3.0
+
4 / 5
0
0.8
Numeric Display Formats
Numbers in Action | 115
str and repr Display Formats
print repr str >>> num = 1 / 3 >>> repr(num) '0.33333333333333331' >>> str(num) '0.333333333333' repr str print str repr strComparisons: Normal and Chained
>>> 1 < 2 True >>> 2.0 >= 1 True >>> 2.0 == 2.0 True >>> 2.0 != 2.0 False
(A < B < C)
B
A
C
(A < B and B <
C)
116 | Chapter 5: Numeric Types
>>> X = 2 >>> Y = 4 >>> Z = 6
Y
>>> X < Y < Z True >>> X < Y and Y < Z True >>> X < Y > Z False >>> X < Y and Y > Z False >>> 1 < 2 < 3.0 < 4 True >>> 1 > 2 > 3.0 > 4 False >>> 1 == 2 < 3 False1 == 2 False
0 < 3
True
True
False
Division: Classic, Floor, and True
X / Y
X // Y
/
//
/
//
/
//
C:\misc> C:\Python30\python >>> >>> 10 / 4 2.5 >>> 10 // 4 2 >>> 10 / 4.0 2.5 >>> 10 // 4.0 2.0 C:\misc> C:\Python26\python >>> >>> 10 / 4 2 >>> 10 // 4 2 >>> 10 / 4.0 2.5 >>> 10 // 4.0 2.0//
/
//
118 | Chapter 5: Numeric Types
Supporting either Python
/
//
float
/
X = Y // Z X = Y / float(Z)/
__future__
float
C:\misc> C:\Python26\python>>> from __future__ import division >>> 10 / 4
2.5 >>> 10 // 4 2
Floor versus truncation
//
math
>>> import math >>> math.floor(2.5) 2 >>> math.floor(-2.5) -3 >>> math.trunc(2.5) 2 >>> math.trunc(-2.5) -2 C:\misc> c:\python30\python >>> 5 / 2, 5 / −2 (2.5, −2.5) >>> 5 // 2, 5 // −2 (2, −3) >>> 5 / 2.0, 5 / −2.0 (2.5, −2.5)Numbers in Action | 119
>>> 5 // 2.0, 5 // −2.0 (2.0, −3.0)
/
C:\misc> c:\python26\python >>> 5 / 2, 5 / −2 (2, −3) >>> 5 // 2, 5 // −2 (2, −3) >>> 5 / 2.0, 5 / −2.0 (2.5, −2.5) >>> 5 // 2.0, 5 // −2.0 (2.0, −3.0)math.trunc
round
C:\misc> c:\python30\python >>> import math >>> 5 / −2 −2.5 >>> 5 // −2 -3 >>> math.trunc(5 / −2) −2 C:\misc> c:\python26\python >>> import math >>> 5 / float(−2) −2.5 >>> 5 / −2, 5 // −2 (−3, −3) >>> math.trunc(5 / float(−2)) −2Why does truncation matter?
>>> (5 / 2), (5 / 2.0), (5 / −2.0), (5 / −2) (2.5, 2.5, −2.5, −2.5) >>> (5 // 2), (5 // 2.0), (5 // −2.0), (5 // −2) (2, 2.0, −3.0, −3) >>> (9 / 3), (9.0 / 3), (9 // 3), (9 // 3.0) (3.0, 3.0, 3, 3.0) >>> (5 / 2), (5 / 2.0), (5 / −2.0), (5 / −2) (2, 2.5, −2.5, −3)
120 | Chapter 5: Numeric Types
>>> (5 // 2), (5 // 2.0), (5 // −2.0), (5 // −2) (2, 2.0, −3.0, −3) >>> (9 / 3), (9.0 / 3), (9 // 3), (9 // 3.0) (3, 3.0, 3, 3.0)
/
//
while
/
from
Integer Precision
>>> 999999999999999999999999999999 + 1 1000000000000000000000000000000 >>> 999999999999999999999999999999 + 1 1000000000000000000000000000000L >>> 2 ** 200 1606938044258990275541962092341162602522202993782792835301376 >>> 2 ** 200 1606938044258990275541962092341162602522202993782792835301376LNumbers in Action | 121
Complex Numbers
j
J
+
2
−3
2 + −3j
>>> 1j * 1J (-1+0j) >>> 2 + 1j * 3 (2+3j) >>> (2 + 1j) * 3 (6+3j)cmath
math
Hexadecimal, Octal, and Binary Notation
>>> 0o1, 0o20, 0o377 (1, 16, 255) >>> 0x01, 0x10, 0xFF (1, 16, 255) >>> 0b1, 0b10000, 0b11111111 (1, 16, 255)
0o377
0xFF
0b11111111
255
>>> oct(64), hex(64), bin(64) ('0100', '0x40', '0b1000000')