• No results found

Python

N/A
N/A
Protected

Academic year: 2021

Share "Python"

Copied!
1213
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)
(3)
(4)

FOURTH EDITION

Learning Python

(5)

Learning Python, Fourth Edition

Editor:

Production Editor:

Copyeditor:

Production Services:

Indexer:

Cover Designer:

Interior Designer:

Illustrator:

Printing History:

www.it-ebooks.info

(6)
(7)
(8)

Table of Contents

Preface . . . xxxi

Part I. Getting Started

1. A Python Q&A Session . . . 3

(9)

2. How Python Runs Programs . . . 23

3. How You Run Programs . . . 35

viii | Table of Contents

(10)

Part II. Types and Operations

4. Introducing Python Object Types . . . 75

(11)

5. Numeric Types . . . 105

6. The Dynamic Typing Interlude . . . 143

x | Table of Contents

(12)

7. Strings . . . 155

8. Lists and Dictionaries . . . 197

(13)

9. Tuples, Files, and Everything Else . . . 225

Part III. Statements and Syntax

10. Introducing Python Statements . . . 261

xii | Table of Contents

(14)

11. Assignments, Expressions, and Prints . . . 279

12. if Tests and Syntax Rules . . . 311

(15)

13. while and for Loops . . . 327

14. Iterations and Comprehensions, Part 1 . . . 351

xiv | Table of Contents

(16)

15. The Documentation Interlude . . . 375

Part IV. Functions

16. Function Basics . . . 395

(17)

17. Scopes . . . 407

18. Arguments . . . 435

xvi | Table of Contents

(18)

19. Advanced Function Topics . . . 463

20. Iterations and Comprehensions, Part 2 . . . 485

(19)

Part V. Modules

21. Modules: The Big Picture . . . 529

22. Module Coding Basics . . . 543

xviii | Table of Contents

(20)

23. Module Packages . . . 561

24. Advanced Module Topics . . . 583

(21)

Part VI. Classes and OOP

25. OOP: The Big Picture . . . 611

26. Class Coding Basics . . . 625

xx | Table of Contents

(22)

27. A More Realistic Example . . . 643

28. Class Coding Details . . . 681

(23)

29. Operator Overloading . . . 705

xxii | Table of Contents

(24)

30. Designing with Classes . . . 737

31. Advanced Class Topics . . . 773

(25)

Part VII. Exceptions and Tools

32. Exception Basics . . . 825

33. Exception Coding Details . . . 835

xxiv | Table of Contents

(26)

34. Exception Objects . . . 857

35. Designing with Exceptions . . . 873

(27)

Part VIII. Advanced Topics

36. Unicode and Byte Strings . . . 895

xxvi | Table of Contents

(28)

37. Managed Attributes . . . 941

(29)

38. Decorators . . . 983

xxviii | Table of Contents

(30)

39. Metaclasses . . . 1051

Part IX. Appendixes

A. Installation and Configuration . . . 1089

B. Solutions to End-of-Part Exercises . . . 1101

Index . . . 1139

(31)
(32)

Preface

(33)

About This Fourth Edition

print

Coverage for Both 3.0 and 2.6

print

print

nonlocal

format

xxxii | Preface

(34)

format with

New Chapters

(35)

Changes to Existing Material

map

zip

__contains__ __bool__

__index__

format

exec

xxxiv | Preface

(36)

Specific Language Extensions in 2.6 and 3.0

list

Extension

Covered in chapter(s)

The

print

function in 3.0

11

The

nonlocal x,y

statement in 3.0

17

The

str.format

method in 2.6 and 3.0

7

String types in 3.0:

str

for Unicode text,

bytes

for 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

,

zip

14

,

20

Dictionary views in 3.0:

D.keys

,

D.values

,

D.items

8

,

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)->int

19

Keyword-only arguments in 3.0:

def f(a, *b, c, **d)

18

,

20

Extended sequence unpacking in 3.0:

a, *b = seq

11

,

13

Relative import syntax for packages enabled in 3.0:

from .

23

Context managers enabled in 2.6 and 3.0:

with

/

as

33

,

35

Exception syntax changes in 3.0:

raise

,

except

/

as

, superclass

33

,

34

(37)

Extension

Covered in chapter(s)

Exception chaining in 3.0:

raise e2 from e1

33

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:

@property

37

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

io

module classes)

9

X.next X.__next__

, called by

next(X)

14

,

20

,

29

X.__getslice__ X.__getitem__

passed a

slice

object

7

,

29

X.__setslice__ X.__setitem__

passed a

slice

object

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

(38)

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=transform

or

reverse=True

8

Dictionary

<

,

>

,

<=

,

>=

Compare

sorted(D.items())

(or loop code)

8

,

9

types.ListType list

(

types

is 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

,

–tt

command-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

,

Queue

modules

_thread

,

queue

(both renamed)

17

anydbm

module

dbm

(renamed)

27

cPickle

module

_pickle

(renamed, used automatically)

9

os.popen2/3/4 subprocess.Popen

(

os.popen

retained)

14

String-based exceptions

Class-based exceptions (also required in 2.6)

32

,

33

,

34

(39)

Removed

Replacement

Covered in chapter(s)

String module functions

String object methods

7

Unbound methods

Functions (

staticmethod

to 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

(40)

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

(41)

The Third Edition’s Structural Changes

The Third Edition’s Scope Changes

xl | Preface

(42)

About This Book

This Book’s Prerequisites

This Book’s Scope and Other Books

(43)

xlii | Preface

(44)

This Book’s Style and Structure

(45)

__name__

xliv | Preface

(46)

Book Updates

^H^H^H

About the Programs in This Book

Using Code Examples

(47)

Font Conventions

Constant width

Constant width bold

Constant width italic

<Constant width>

xlvi | Preface

(48)

% C:\Python30> % >>> ... #

Safari® Books Online

How to Contact Us

(49)

Acknowledgments

xlviii | Preface

(50)
(51)
(52)

PART I

(53)
(54)

CHAPTER 1

A Python Q&A Session

Why Do People Use Python?

(55)

Software Quality

4 | Chapter 1: A Python Q&A Session

(56)

Developer Productivity

Is Python a “Scripting Language”?

import this

(57)

6 | Chapter 1: A Python Q&A Session

(58)

OK, but What’s the Downside?

Who Uses Python Today?

(59)

8 | Chapter 1: A Python Q&A Session

(60)

What Can I Do with Python?

Systems Programming

GUIs

(61)

Internet Scripting

Component Integration

10 | Chapter 1: A Python Q&A Session

(62)

Database Programming

pickle

Rapid Prototyping

Numeric and Scientific Programming

(63)

Gaming, Images, Serial Ports, XML, Robots, and More

xml

xmlrpclib

How Is Python Supported?

12 | Chapter 1: A Python Q&A Session

(64)

What Are Python’s Technical Strengths?

It’s Object-Oriented

It’s Free

(65)

It’s Portable

14 | Chapter 1: A Python Q&A Session

(66)

It’s Powerful

(67)

It’s Mixable

It’s Easy to Use

16 | Chapter 1: A Python Q&A Session

(68)

It’s Easy to Learn

It’s Named After Monty Python

How Does Python Stack Up to Language X?

(69)

Chapter Summary

18 | Chapter 1: A Python Q&A Session

(70)

Test Your Knowledge: Quiz

import this

Test Your Knowledge: Answers

(71)

import this

Python Is Engineering, Not Art

20 | Chapter 1: A Python Q&A Session

(72)
(73)
(74)

CHAPTER 2

How Python Runs Programs

Introducing the Python Interpreter

(75)

Program Execution

The Programmer’s View

print('hello world') print(2 ** 100)

print

print

24 | Chapter 2: How Python Runs Programs

(76)
(77)

print

hello world

1267650600228229401496703205376

C:\temp> python script0.py hello world

1267650600228229401496703205376

Python’s View

Byte code compilation

26 | Chapter 2: How Python Runs Programs

(78)

The Python Virtual Machine (PVM)

Performance implications

(79)

Development implications

eval

exec

28 | Chapter 2: How Python Runs Programs

(80)

Execution Model Variations

Python Implementation Alternatives

CPython

Jython

(81)

IronPython

Execution Optimization Tools

The Psyco just-in-time compiler

30 | Chapter 2: How Python Runs Programs

(82)

The Shedskin C++ translator

(83)

Frozen Binaries

32 | Chapter 2: How Python Runs Programs

(84)

Other Execution Options

Future Possibilities?

(85)

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#

(86)

CHAPTER 3

How You Run Programs

exec

The Interactive Prompt

python

(87)

% 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\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.

>>>

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

(88)

Running Code Interactively

>>>

>>>

print

print

% python >>> print('Hello world!') Hello world! >>> print(2 ** 8) 256

print

>>>

2 ** 8

>>> lumberjack = 'okay' >>> lumberjack 'okay' >>> 2 ** 8 256 >>> %

lumberjack

2 ** 8

print

(89)

print

>>>

Why the Interactive Prompt?

Experimenting

'Spam!' * 8

>>> 'Spam!' * 8 'Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!'

*

38 | Chapter 3: How You Run Programs

(90)

>>> 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

(91)

print

print

print

print

...

>>>

...

>>>

...

sys

40 | Chapter 3: How You Run Programs

(92)

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

(93)

python

A First Script

import sys print(sys.platform) print(2 ** 100) x = 'Spam!' print(x * 8)

print

x

sys.platform

sys

42 | Chapter 3: How You Run Programs

(94)

#

#

Running Files with Command Lines

python

% python script1.py win32 1267650600228229401496703205376 Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!

PATH

print

% python script1.py > saveit.txt

(95)

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

(96)

python script1.py

python script1

import

import script1

python d:\tests\spam.py

import spam

print

print

print

print

(97)

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

(98)

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

(99)

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

(100)

The input Trick

input

raw_input

import sys print(sys.platform) print(2 ** 100) x = 'Spam!' print(x * 8) input()

input

(101)

input

print

input

input('Press

Enter to exit')

nextinput = input()

python spam.py

< input.txt

print

input

raw_input() input() input input raw_input input eval(input())

Other Icon-Click Limitations

input

input

50 | Chapter 3: How You Run Programs

(102)

try

Module Imports and Reloads

import

input

C:\misc> c:\python30\python >>> import script1 win32 1267650600228229401496703205376 Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!

(103)

>>> 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

print

2 ** 16

import

reload

reload

reload

import

reload

import

reload

reload

52 | Chapter 3: How You Run Programs

(104)

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

(105)

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)

print

import from .py import

54 | Chapter 3: How You Run Programs

(106)

% 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

(107)

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

(108)

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 PYTHONPATH

(109)

import

execfile('module.py') exec(open('module.py')) exec(open('module.py').read()) exec exec

The IDLE User Interface

exec

IDLE Basics

58 | Chapter 3: How You Run Programs

(110)

>>>

yum tkinter

idle

(111)

Using IDLE

60 | Chapter 3: How You Run Programs

(112)
(113)

-n

idle.py -n

cd

import

Advanced IDLE Tools

62 | Chapter 3: How You Run Programs

(114)

Other IDEs

(115)

Other Launch Options

Embedding Calls

64 | Chapter 3: How You Run Programs

(116)

#include <Python.h> ...

Py_Initialize(); PyRun_SimpleString("x = 'brave ' + 'sir robin'");

Frozen Binary Executables

Text Editor Launch Options

(117)

Still Other Launch Options

os.popen os.system

Future Possibilities?

Which Option Should I Use?

66 | Chapter 3: How You Run Programs

(118)

Debugging Python Code

print print print # print print

(119)

Chapter 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

(120)

Test Your Knowledge: Answers

python

PATH

cd

python

C:\Python30\python

exec

#!

input

(121)

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

(122)

#!

#!

2 ** 500

1 / 0

L = [1, 2] L.append(L) L

(123)

help

72 | Chapter 3: How You Run Programs

(124)

PART II

(125)
(126)

CHAPTER 4

Introducing Python Object Types

(127)

Why Use Built-in Types?

76 | Chapter 4: Introducing Python Object Types

(128)

Python’s Core Data Types

Object type

Example literals/creation

Numbers

1234

,

3.1415

,

3+4j

,

Decimal

,

Fraction

Strings

'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,

None

Program 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

(129)

Numbers

+

*

**

78 | Chapter 4: Introducing Python Object Types

(130)

>>> 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

print

>>> import math >>> math.pi 3.1415926535897931 >>> math.sqrt(85) 9.2195444572928871

math

random

>>> import random >>> random.random() 0.59268735266273953 >>> random.choice([1, 2, 3, 4]) 1

Numbers | 79

(131)

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

(132)

>>> 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

(133)

'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

(134)

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()

(135)

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

(136)

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

(137)

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

(138)

>>> 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

(139)

>>> 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] 6

Comprehensions

88 | Chapter 4: Introducing Python Object Types

(140)

>>> 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

(141)

>>> 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

(142)

>>> 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}

(143)

>>> 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

(144)

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 => 3

sorted

sorted

>>> D {'a': 1, 'c': 3, 'b': 2} >>> for key in sorted(D): print(key, '=>', D[key]) a => 1 b => 2 c => 3

for

for

Dictionaries | 93

(145)

key

for

while

for

>>> for c in 'spam': print(c.upper()) S P A M

while

>>> 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

(146)

>>> 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

(147)

if

for

if

>>> 'f' in D False >>> if not 'f' in D: print('missing') missing

if

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 0

Tuples

>>> T = (1, 2, 3, 4) >>> len(T) 4 >> T + (5, 6) (1, 2, 3, 4, 5, 6) >>> T[0] 1

96 | Chapter 4: Introducing Python Object Types

(148)

>>> 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

(149)

'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

(150)

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)

(151)

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

(152)

>>> 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

(153)

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

(154)

class

Chapter Summary

Test Your Knowledge: Quiz

(155)

Test Your Knowledge: Answers

None

'spam'

open

+

104 | Chapter 4: Introducing Python Object Types

(156)

CHAPTER 5

Numeric Types

Numeric Type Basics

(157)

Numeric Literals

Literal

Interpretation

1234

,

−24

,

0

,

99999999999999

Integers (unlimited size)

1.23

,

1.

,

3.14e-10

,

4E210

,

4.0e+210

Floating-point numbers

0177

,

0x9ff

,

0b101010

Octal, hex, and binary literals in 2.6

0o177

,

0x9ff

,

0b101010

Octal, hex, and binary literals in 3.0

3+4j

,

3.0+4.0j

,

3J

Complex number literals

e

E

106 | Chapter 5: Numeric Types

(158)

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)

(159)

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

(160)

Operators

Description

yield x

Generator function

send

protocol

lambda args: expression

Anonymous function generation

x if y else z

Ternary selection (

x

is evaluated only if

y

is true)

x or y

Logical OR (

y

is evaluated only if

x

is false)

x and y

Logical AND (

y

is evaluated only if

x

is true)

not x

Logical negation

x in y

,

x not in y

Membership (iterables, sets)

x is y

,

x is not y

Object identity tests

x < y

,

x <= y

,

x > y

,

x >= y x == y

,

x != y

Magnitude 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 >> y

Shift

x

left or right by

y

bits

x + y x – y

Addition, concatenation;

Subtraction, set difference

x * y x % y x / y

,

x // y

Multiplication, repetition;

Remainder, format;

Division: true and floor

−x

,

+x

Negation, 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

(161)

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

(162)

Mixed operators follow operator precedence

A * B + C * D

X + 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

(163)

>>> int(3.1415) 3

>>> float(3) 3.0

Preview: Operator overloading and polymorphism

+

[i]

112 | Chapter 5: Numeric Types

(164)

+

+

Numbers in Action

Variables and Basic Expressions

a

b

a

b

% python >>> a = 3 >>> b = 4

#

#

Numbers in Action | 113

(165)

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 * 2

Traceback (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

(166)

a

3.0

+

4 / 5

0

0.8

Numeric Display Formats

print

print

>>> b / (2.0 + a) 0.80000000000000004 >>> print(b / (2.0 + a)) 0.8

print

print

>>> 1 / 2.0 0.5

print

>>> num = 1 / 3.0 >>> num 0.33333333333333331 >>> print(num) 0.333333333333 >>> '%e' % num '3.333333e-001' >>> '%4.2f' % num '0.33' >>> '{0:4.2f}'.format(num) '0.33'

Numbers in Action | 115

(167)

str and repr Display Formats

print repr str >>> num = 1 / 3 >>> repr(num) '0.33333333333333331' >>> str(num) '0.333333333333' repr str print str repr str

Comparisons: 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

(168)

>>> 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 False

1 == 2 False

0 < 3

True

True

False

Division: Classic, Floor, and True

X / Y

X // Y

(169)

/

//

/

//

/

//

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

(170)

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

(171)

>>> 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)) −2

Why 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

(172)

>>> (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 1606938044258990275541962092341162602522202993782792835301376L

Numbers in Action | 121

(173)

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')

122 | Chapter 5: Numeric Types

References

Related documents

In these slides, we either run Python in a Terminal (similar to using MATLAB’s command window), or use a text editor for the code and run the script in the Terminal for an

To start programming in Python, we have to learn how to type the source code and save it to a le, using a text editor program.. We also need to know how to open a Command Terminal

Duncan Digital Design Manager CREATIVE SERVICES Kathryn Creative Services Manager Kristina Designer Eduardo Designer Janet Assistant Production Editor Hilly Production

The LdM Fashion Design Certificate offers students the opportunity to explore a variety of career disciplines: Fashion Designer, Fashion Editor, Stylist, Illustrator,

How to Think Like a Computer Scientist: Learning with Python 3 Documentation, Release 3rd Edition..

This PDF file contains pages extracted from Python Testing with pytest, Second Edition, published by the Pragmatic Bookshelf.. For more information or to purchase a paperback or

The Copy Center Technician, Graphic Designer, Illustrator, and Production Artist assessments are end-of- program assessments for students in Advertising Design programs..

She has worked as a graphic designer and art editor for several companies, and recently started working as a freelance illustrator for books, magazines, children's games,