作者简介:Magnus Lie Hetland(http://hetland.org/)挪威科技大学副教授。
第一章基础知识
str()、repr()、反引号的使用与区别:str 和int、long一样是一种类型,而repr()则是一个函数。
input()和raw_input()的区别:input()会把字符串当字符串,数值当数值;而raw_input()则是把原始数据存放在字符串里。
长字符串、原始字符串和Unicode
长字符串的使用:
>>> print '''adfjkl adfkldsj'''
adfjkl adfkldsj
原始字符串: print r'C:\nowhere'在字符串前添加r,但也要注意结尾处的\
Unicode:u"Hello,world"
第二章:列表和元组
python包含6种内建的序列,最常用的是:列表和元组。通用的序列操作有:索引、分片、加、乘以及成员资格(in)。
内建函数:len min max
总共的内联函数有:
|
|
Built-in Functions |
|
|
abs() |
divmod() |
input() |
open() |
staticmethod() |
all() |
enumerate() |
int() |
ord() |
str() |
any() |
eval() |
isinstance() |
pow() |
sum() |
basestring() |
execfile() |
issubclass() |
print() |
super() |
bin() |
file() |
iter() |
property() |
tuple() |
bool() |
filter() |
len() |
range() |
type() |
bytearray() |
float() |
list() |
raw_input() |
unichr() |
callable() |
format() |
locals() |
reduce() |
unicode() |
chr() |
frozenset() |
long() |
reload() |
vars() |
classmethod() |
getattr() |
map() |
repr() |
xrange() |
cmp() |
globals() |
max() |
reversed() |
zip() |
compile() |
hasattr() |
memoryview() |
round() |
__import__() |
complex() |
hash() |
min() |
set() |
apply() |
delattr() |
help() |
next() |
setattr() |
buffer() |
dict() |
hex() |
object() |
slice() |
coerce() |
dir() |
id() |
oct() |
sorted() |
intern() |
列表(python的苦力)的操作:
append
count
extend
index
insert
pop
remove
reverse
sort
sorted(x)
高级排序
元组:不可变序列
元组的意义:元组可以在映射中当作键使用,而列表不行。
第三章:使用字符串
%格式化字符串
>>> format="h %s %s enough "
>>> v=('a','b')
>>> print format % v h a b enough
模板字符串
from string import Template
>>> s=Template('$x,glorious $x')
>>> s.substitute(x='lsm')
%字符,转换开始
转换标志:-表示左对齐,+表示在转换前加上正负号,""表示在字符串前保留空格,0表示值若不够则用0填充
转换类型:
int(x [,base ]) 将x转换为一个整数
long(x [,base ]) 将x转换为一个长整数
float(x ) 将x转换到一个浮点数
complex(real [,imag ]) 创建一个复数
str(x ) 将对象 x 转换为字符串
repr(x ) 将对象 x 转换为表达式字符串
eval(str ) 用来计算在字符串中的有效Python表达式,并返回一个对象
tuple(s ) 将序列 s 转换为一个元组
list(s ) 将序列 s 转换为一个列表
chr(x ) 将一个整数转换为一个字符
unichr(x ) 将一个整数转换为Unicode字符
ord(x ) 将一个字符转换为它的整数值
hex(x ) 将一个整数转换为一个十六进制字符串
oct(x ) 将一个整数转换为一个八进制字符串
find()
join
>>> p='+'
>>>qq=["a","b"]
>>> qq.join(p)
Traceback (most recent call last): File "<pyshell#73>", line 1, in <module> qq.join(p) AttributeError: 'list' object has no attribute 'join'
>>> p.join(qq)
'a+b'
>>> p
lower
replace
split
>>> import string
>>> string .capwords("that's all ,folk")
"That's All ,folk"
strip
去除两边的空格
translate()
>>> test='this is an incredible test'
>>> from string import maketrans
>>> table=maketrans('a', 'e')
>>> test.translate(table)
'this is en incredible test'
第四章:字典,当索引不好用时
基本的字典操作:
len(d)
d[k]
del d[k]
字典的格式化字符串
字典方法
clear()
copy()
fromkeys()
get()
has_key()
items()iteritems()
keys()iterkeys()
pop() popitem()
setdefault() update()
values() itervalues()
第五章: 条件、循环和其他语句
复制魔法:
序列解包
>>> x,y,z=1,2,3
>>> x
1
>>> y
2
>>> z
3
链式赋值
>>> x=y=z=4
>>> x
4
增量赋值
代码嵌套
name=raw_input("what's your name?")
if name.endswith('ming'):
if name.startswith('Mr'):
print 'hello,Mr.Ming'
elif name.startswith('Mrs'):
print 'hello,mrs,ming'
else:
print 'hello'
else:
print 'hello ,stranger'
断言的使用
一些迭代工具
并行迭代
names=['a','b','c']
ages=[1,2,3]
#for i in range(len(names)):
# print names[i],'is',ages[i],'years old'
for name,age in zip(names,ages):
print name,'is',age,'yeas old'
编号迭代
翻转和排序迭代
列表推导式---轻量级循环
[x*x for x in range(10)]
使用exec和eval执行和求值字符串
exec
第六章:抽象
callable():判断函数是否可以调用
记录函数:文档字符串
参数存储在局部作用域内,不可改变
关键参数与默认值
收集参数
作用域的定义
globals()['parameter']
global
递归的使用
python中“函数式编程”有用的函数
map
filter
reduce
>>> map(str,range(10))
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
>>> map(str,range(10))
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
>>> def funx(x):
return x.isalnum()
>>> seq=["af","12","^&*"]
>>> filter(funx,seq)
['af', '12']
>>> filter(lambda x: x.isalnum(),seq)
['af', '12']
第七章:更加抽象
类的命名空间
指定超类:
class Filter:
def init(self):
self.blocked=[]
def filter(self,sequence):
return [x for x in sequence if x not in self.blocked]
class SpAFilter(Filter):
def init(self):
self.blocked=['SPAM']
>>> f=Filter()
>>> f.init()
>>> f.filter([1,2,3])
[1, 2, 3]
>>> s=SpAFilter()
>>> s.init()
>>> s.filter(['SPAM','asfd'])
['asfd']
调查继承
issubclass()
第八章:异常
import exceptions
BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
+-- StopIteration
+-- StandardError
| +-- BufferError
| +-- ArithmeticError
| | +-- FloatingPointError
| | +-- OverflowError
| | +-- ZeroDivisionError
| +-- AssertionError
| +-- AttributeError
| +-- EnvironmentError
| | +-- IOError
| | +-- OSError
| | +-- WindowsError (Windows)
| | +-- VMSError (VMS)
| +-- EOFError
| +-- ImportError
| +-- LookupError
| | +-- IndexError
| | +-- KeyError
| +-- MemoryError
| +-- NameError
| | +-- UnboundLocalError
| +-- ReferenceError
| +-- RuntimeError
| | +-- NotImplementedError
| +-- SyntaxError
| | +-- IndentationError
| | +-- TabError
| +-- SystemError
| +-- TypeError
| +-- ValueError
| +-- UnicodeError
| +-- UnicodeDecodeError
| +-- UnicodeEncodeError
| +-- UnicodeTranslateError
+-- Warning
+-- DeprecationWarning
+-- PendingDeprecationWarning
+-- RuntimeWarning
+-- SyntaxWarning
+-- UserWarning
+-- FutureWarning
+-- ImportWarning
+-- UnicodeWarning
+-- BytesWarning
自定义异常类
没有参数
class muffledCalculator:
muffled=False
def calc(self,expr):
try:
return eval(expr)
except ZeroDivisionError:
if self.muffled:
print 'Division by zero is illegal'
else:
raise
用一个块捕捉多个异常
捕捉对象;
try:
x=input('Enter the 1st number:')
y=input('Enter the 2nd number:')
print x/y
except(ZeroDivisionError,TypeError),e:
print e
try:
except:
else:
finally:
异常之禅?
第九章:魔法方法、属性和迭代器
构造函数
绑定超类:
class Bird:
def __init__(self):
self.hungry=True
def eat(self):
if self.hungry:
print 'aaa'
self.hungry=False
else:
print 'No thanks'
class SongBird(Bird):
def __init__(self):
Bird.__init__(self)
self.sound='Squawk'
def sing(self):
print self.sound
使用super函数
__metaclass__=type
class Bird:
def __init__(self):
self.hungry=True
def eat(self):
if self.hungry:
print 'aaa'
self.hungry=False
else:
print 'No thanks'
class SongBird(Bird):
def __init__(self):
# Bird.__init__(self)
super(SongBird,self).__init__()
self.sound='Squawk'
def sing(self):
print self.sound
成员访问
基本的序列和映射规则
__len__(self)
__getitem__(self,key)
__setitem__(self,key)
__delitem__(self,key)
def checkIndex(key):
if not isinstance(key,(int,long)):raise TypeError
if key<0:raise IndexError
class ArithmeticSequence:
def __init__(self,start=0,step=1):
self.start=start
self.step=step
self.changed={}
def __getitem__(self,key):
checkIndex(key)
try:return self.changed[key]
except KeyError:
return self.start+key*self.step
def __setitem__(self,key,value):
checkIndex(key)
self.changed[key]=value
def __delitem__(self,key):
checkIndex(key)
del self.changed[key]
子类化列表,字典和字符串
标准库里有3个关于序列和映射规则(UserList,UserString,UserDict)
class CounterList(list):
def __init__(self,*args):
super(CounterList,self).__init__(*args)
self.counter=0
def __getitem__(self,index):
self.counter+=1
return super(CounterList,self).__getitem__(index)
property函数
property()其实不是一个真正的函数,它是拥有许多特殊方法的类,也正是这个方法完成了所有的工作。
__metaclass__=type
class Rectangle:
def __init__(self):
self.width=0
self.height=0
def setSize(self,size):
self.width,self.height=size
def getSize(self):
return self.width,self.height
size=property(getSize,setSize)
使用__getattr__ ___setattr__
__metaclass__=type
class Rectangle:
def __init__(self):
self.width=0
self.height=0
def __setattr__(self,name,value):
if name=='size':
self.width,self.height=value
else:
self.__dict__[name]=value
def __getattr__(self,name):
if name=='size':
return self.width,self.height
else:
raise AttributeError
迭代器
class Fibs:
def __init__(self):
self.a=0
self.b=1
def next(self):
self.a,self.b=self.b,self.a+self.b
return self.a
def __iter__(self):
return self
>>> fibs=Fibs()
>>> for f in fibs:
if f>1000:
print f
break
1597
>>> it=iter([1,2,4])
>>> it.next()
1
>>> it.next()
2
生成器
def flatten(nested):
for sublist in nested:
for element in sublist:
yield element
递归
def flatten(nested):
try:
for sublist in nested:
for element in flatten(sublist):
yield element
except TypeError:
yield nested
生成器由两部分组成:生成器的函数和生成器的迭代器
不适用yield的模拟生成器
def flatten(nested):
result=[]
try:
try:nested+''
except TypeError:pass
else: raise TypeError
for sublist in nested:
for element in flatten(sublist):
result.append(element)
except TypeError:
result.append(nested)
return result
八皇后问题解决
def conflict(state,nextX):
nextY=len(state)
for i in range(nextY):
if abs(state[i]-nextX)in(0,nextY-i):
return True
return False
def queens(num=8,state=()):
for pos in range(num):
if not conflict(state,pos):
if len(state)==num-1:
yield(pos,)
else:
for result in queens(num,state+(pos,)):
yield(pos,)+result
def prettyprint(solution):
def line(pos,length=len(solution)):
return '.'*(pos)+'X'+'.'*(length-pos-1)
for pos in solution:
print line(pos)
>>> import random
>>> prettyprint(random.choice(list(queens(8))))
第十章:模块
>>> import sys,pprint
>>> pprint.pprint(sys.path)
探究模块
自定义模块
>>> import sys
>>> sys.path.append('E:')
>>> import hello2
>>> hello2.hello()
Hello,world
>>> import sys,pprint
>>> pprint.pprint(sys.path)
>>> import copy
>>> [n for n in dir(copy) if not n.startswith('_')]
['Error', 'PyStringMap', 'copy', 'deepcopy', 'dispatch_table', 'error', 'name', 't', 'weakref']
sys模块
argv
exit
modules
path
platform
stdin
stdout
stderr
os模块
fileinput模块
import fileinput
for line in fileinput.input(inplace=True):
line=line.rstrip()
num=fileinput.lineno()
print '%-40s#%2i'% (line,num)
集合、堆、双端队列
堆
>>> set(range(10))
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Instances of
Set
and
ImmutableSet
both provide the following operations:
Operation |
Equivalent |
Result |
len(s) |
|
cardinality of set s |
x in s |
|
test x for membership ins |
x not in s |
|
test x for non-membership ins |
s.issubset(t) |
s <= t |
test whether every element in s is in t |
s.issuperset(t) |
s >= t |
test whether every element in t is in s |
s.union(t) |
s | t |
new set with elements from both s and t |
s.intersection(t) |
s & t |
new set with elements common to s and t |
s.difference(t) |
s - t |
new set with elements in s but not int |
s.symmetric_difference(t) |
s ^ t |
new set with elements in either s or t but not both |
s.copy() |
|
new set with a shallow copy of s |
堆
heapq.heappush
(
heap,item
)
Push the value item onto theheap, maintaining the heap invariant.
heapq.heappop
(
heap
)
Pop and return the smallest item from theheap, maintaining the heap invariant. If the heap is empty,
IndexError
is raised.
heapq.heappushpop
(
heap,item
)
Push item on the heap, then pop and return the smallest item from theheap. The combined action runs more efficiently than
heappush()
followed by a separate call to
heappop()
.
New in version 2.6.
heapq.heapify
(
x
)
Transform list x into a heap, in-place, in linear time.
heapq.heapreplace
(
heap,item
)
Pop and return the smallest item from theheap, and also push the newitem. The heap size doesn’t change. If the heap is empty,
IndexError
is raised.
>>> from heapq import *
>>> from random import shuffle
>>> data=range(10)
>>> shuffle(data)
>>> heap=[]
>>> for n in data:
heappush(heap,n)
双端队列
>>> from collections import deque
>>> q=deque(range(5))
>>> q
deque([0, 1, 2, 3, 4])
>>> q.append(9)
>>> q
deque([0, 1, 2, 3, 4, 9])
>>> q.appendleft(6)
>>> q
deque([6, 0, 1, 2, 3, 4, 9])
>>> q.pop()
9
>>> q
deque([6, 0, 1, 2, 3, 4])
time模板
random模板
shelve模板
import sys,shelve
def store_person(db):
pid=raw_input('Enter ID:')
person={}
person['name']=raw_input('Name:')
person['age']=raw_input('Age:')
person['phone']=raw_input('Phone')
db[pid]=person
def lookup_person(db):
pid=raw_input('Enter ID:')
field=raw_input('what would you like to know?(name,age,phone)')
field=field.strip().lower()
print field.capitalize()+':',db[pid][field]
def print_help():
print 'commads:'
print 'store'
print 'lookup'
print 'quit'
print '?'
def enter_command():
cmd=raw_input('Enter Command?')
cmd=cmd.strip().lower()
return cmd
def main():
database=shelve.open('E:\\database.dat')
try:
while True:
cmd=enter_command()
if cmd=='store':
store_person(database)
if cmd=='lookup':
lookup_person(database)
if cmd=='?':
print_help()
elif cmd=='quit':
return
finally:
database.close()
if __name__=='__main__':main()
re模块
complie()
search()
match()
split()
findall()
sub()
escape()
>>> m=re.match(r'www\.(.*)\..{3}','www.python.org')
>>> m.group(1)
>>> re.split('[.]+',text,maxsplit=2)
['alpha,beta,gamma,,, delta']
模板系统示例
第十一章:文件与素材
def process(string):
print 'Processing:',string
f=open(r'E:/hello.py')
char=f.read(1)
while True:
# process(char)
#char=f.read(1)
line=f.readline()
if not line:break
process(line)
f.close()
第十二章:图形界面
下载安装wxpython
第十三章:数据库支持
SQLite PySQLite
第十四章:网络编程
socket模块
urllib和urllib2模块
SocketServer模块
客户端:
from socket import *
host='127.0.0.1'
port=3130
bufsize=1024
addr=(host,port)
client=socket(AF_INET,SOCK_STREAM)
client.connect(addr)
while True:
data=raw_input()
if not data or data =='exit':
break
client.send('%s\r\n' % data)
data=client.recv(bufsize)
if not data:
break
print data.strip()
client.close()
服务器端:
import SocketServer
from SocketServer import StreamRequestHandler as SRH
from time import ctime
host='127.0.0.1'
port=3130
addr=(host,port)
class Servers(SRH):
def handle(self):
print 'get connection from',self.client_address
self.wfile.write('connection %s:%s at %s succeed!'%(host,port,ctime))
while True:
data=self.request.recv(1024)
if not data:break
print data
print 'server is running ...'
server=SocketServer.ThreadingTCPServer(addr,Servers)
server.serve_forever()
多连接
有3种方法可以实现这个目的:分叉、线程、以及异步I/O。
带有select和poll的异步I/O
Twisted网络框架
第十五章:python和万维网
屏幕抓取、CGI和mod_python
第十六章:测试
doctest
doctest
def square(x):
'''
square a number
'''
return x*x
if __name__='__main__':
import doctest,
unitest模板
import unittest,my_math
class ProductTestCase(unittest.TestCase):
def testIntegers(self):
for x in xrange(-10,10):
for y in xrange(-10,10):
p=my_math.product(x,y)
self.failUnless(p==x*y,'Interger failed')
def testFloats(self):
for x in xrange(-10,10):
for y in xrange(-10,10):
x=x/10.0
y=y/10.0
p=my_math.product(x,y)
self.failUnless(p==x*y,'Failed')
if __name__=='__main__':
unittest.main()
使用PyChecker和PyLint检查源代码
第十七章:扩展python
使用Jython和IronPython
使用CPython
SWIG
第十八章:程序打包
Distutils基础
第十九章:好玩的编程
原型设计
配置文件
日志记录