Python基础教程(第2版)读书笔记
作者简介: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=Falsedef 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 etry:except:else:
finally:
异常之禅?
第九章:魔法方法、属性和迭代器
构造函数
绑定超类:
class Bird:def __init__(self):self.hungry=Truedef eat(self):if self.hungry:print 'aaa'self.hungry=Falseelse: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=Truedef eat(self):if self.hungry:print 'aaa'self.hungry=Falseelse: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 TypeErrorif key<0:raise IndexErrorclass ArithmeticSequence:def __init__(self,start=0,step=1):self.start=startself.step=stepself.changed={}def __getitem__(self,key):checkIndex(key)try:return self.changed[key]except KeyError:return self.start+key*self.stepdef __setitem__(self,key,value):checkIndex(key)self.changed[key]=valuedef __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=0def __getitem__(self,index):self.counter+=1return super(CounterList,self).__getitem__(index)
property函数
property()其实不是一个真正的函数,它是拥有许多特殊方法的类,也正是这个方法完成了所有的工作。
__metaclass__=type
class Rectangle:def __init__(self):self.width=0self.height=0def setSize(self,size):self.width,self.height=sizedef getSize(self):return self.width,self.heightsize=property(getSize,setSize)
使用__getattr__ ___setattr__
__metaclass__=type
class Rectangle:def __init__(self):self.width=0self.height=0def __setattr__(self,name,value):if name=='size':self.width,self.height=valueelse:self.__dict__[name]=valuedef __getattr__(self,name):if name=='size':return self.width,self.heightelse:raise AttributeError
迭代器
class Fibs:def __init__(self):self.a=0self.b=1def next(self):self.a,self.b=self.b,self.a+self.breturn self.adef __iter__(self):return self>>> fibs=Fibs()
>>> for f in fibs:if f>1000:print fbreak1597
>>> 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 elementexcept TypeError:yield nested
生成器由两部分组成:生成器的函数和生成器的迭代器
不适用yield的模拟生成器
def flatten(nested):result=[]try:try:nested+''except TypeError:passelse: raise TypeErrorfor 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 Truereturn Falsedef 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,)+resultdef 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 fileinputfor 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,shelvedef 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]=persondef 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':returnfinally: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:',stringf=open(r'E:/hello.py')char=f.read(1)
while True:# process(char)#char=f.read(1)line=f.readline()if not line:breakprocess(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':breakclient.send('%s\r\n' % data)data=client.recv(bufsize)if not data:breakprint data.strip()client.close()
服务器端:
import SocketServer
from SocketServer import StreamRequestHandler as SRH
from time import ctimehost='127.0.0.1'
port=3130
addr=(host,port)class Servers(SRH):def handle(self):print 'get connection from',self.client_addressself.wfile.write('connection %s:%s at %s succeed!'%(host,port,ctime))while True:data=self.request.recv(1024)if not data:breakprint dataprint 'server is running ...'server=SocketServer.ThreadingTCPServer(addr,Servers)
server.serve_forever()
多连接
有3种方法可以实现这个目的:分叉、线程、以及异步I/O。
带有select和poll的异步I/O
Twisted网络框架
第十五章:python和万维网
屏幕抓取、CGI和mod_python
第十六章:测试
doctest
doctestdef square(x):'''
square a number
'''return x*xif __name__='__main__':import doctest,
unitest模板
import unittest,my_mathclass 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.0y=y/10.0p=my_math.product(x,y)self.failUnless(p==x*y,'Failed')if __name__=='__main__':unittest.main()
使用PyChecker和PyLint检查源代码
第十七章:扩展python
使用Jython和IronPython
使用CPython
SWIG
第十八章:程序打包
Distutils基础
第十九章:好玩的编程
原型设计
配置文件
日志记录
Python基础教程(第2版)读书笔记相关推荐
- Python基础教程(第3版)读书笔记:第3章 使用字符串
相关阅读:python字符串-内建函数.转义字符.运算符 第3章 使用字符串 关于字符串与字符串的编码,可参考:廖雪峰的文章 Unicode ( https://www.unicode.org/ ) ...
- Python基础教程(第3版)读书笔记:第2章 列表和元组
文章目录 第2章 列表和元组 2.2 通用的序列操作 2.2.1 索引 2.2.2 切片 2.2.3 序列相加 2.2.4 乘法 读代码,猜运行结果 2.2.5 成员资格 2.3 列表:Python的 ...
- 《Python基础教程(第3版)》笔记:第8章异常
<Python基础教程(第3版)>笔记:异常 重点 第8章 异常 异常对象未被处理(或捕获)时,程序将终止并显示一条错误信息:traceback 每个异常都是某个类的实例, 如何创建异常 ...
- Python基础教程(第3版)》笔记:第6章抽象
Python基础教程(第3版)>笔记:第6章抽象 **斐波那契数列:**每个数都是前两个数的和. fibs = [0,1] for i in range(8):fibs.append(fibs[ ...
- python基础读后感_《python基础教程 》第一章 读书笔记
python是一个简单强大的直译语言,它同样提供交互式编译环境,学起来还算有趣,在学习的过程中,同样体会了动态语言的直接与强大. 第一章 基础知识 一 运行python 在ubuntu终端输入 pyt ...
- Python 基础教程(第二版)读书笔记
Python 基础教程(第二版) 第一章 在 Python 3 中可直接使用长整数,而不必添加 L 或者 l 的后缀. print在 Python 3 中是函数. 在交互式解释器中使用 if 语句,需 ...
- 《python基础教程(第二版)》学习笔记 基础部分(第1章)
<python基础教程(第二版)>学习笔记 基础部分(第1章) python常用的IDE: Windows: IDLE(gui), Eclipse+PyDev; Python(comman ...
- 论书 | 《Python基础教程(第二版)》怎么样?有用吗?
大家好我是本文编辑逻辑熊猫! 对我感兴趣的朋友欢迎关注我的个人公众号"逻辑熊猫带你玩Python"~ 由于笔者能力有限,所以呢就说说书吧,或许以后有机会出版社赞助一下就能给大家送个 ...
- python基础教程第三版电子版百度云-《python基础教程第三版》高清版PDF免费下载...
下载地址1:http://t.cn/EGxO1sW Python基础教程 第3版Python简明教程书籍 Python编程从入门到实践 灵程序设计丛书 <python基础教程第三版>高清版 ...
- python基础教程第三版豆瓣-1024,程序媛/猿请查收!
点击上方蓝字关注我们 节专享福利:1024程序员 本期活动,不仅有赠书福利,且有购书福利,图灵公司联合当当网特意为{印象python}读者们申请了一波购书福利.感兴趣的读者朋友,请下拉至文末,领取福利 ...
最新文章
- VirtualBox下安装rhel5.5 linux系统
- CSS hack浏览器兼容一览表
- 企业网站6个常见的优化漏洞
- 洛谷 2759 奇怪的函数
- CVS配置过程 (部分转)
- UITextField 对比 UITextView
- 1、CSS 提示工具(Tooltip),2、box-sizing: border-box;的作用,3、实例2 - 图像的透明度 - 悬停效果,4、CSS 图像拼合技术,
- 对通用查询组件初始化组织过滤条件
- Java基础-四大特性理解(抽象、封装、继承、多态)
- cpu内存和线程和pool多进程池 Python
- JLNews新闻点评系统--君兰IT
- 计算机网络纠错码,纠错码
- CASS11.0.0.6安装以及教程
- 视频录制后有噪音怎么办?教你简答几步去除视频噪音!
- [精简]快速认识钢琴键盘
- php类的开发语言,php是什么开发语言
- ATFX:美国服务业持续收缩,科技股前景如何?
- java计算移动平均值_多种移动平均计算总结(MA,EMA,SMA,DMA,TMA,WMA)
- 生僻字_tte_linux_ttf_提取字体_打印生僻字_uni
- 年仅28岁的程序员郭宇,宣布从字节跳动辞职,实现财富自由!
热门文章
- Flutter 可选参数方法构建
- Bugku逆向-游戏过关
- 【每日新闻】2017年亚马逊研发投入排世界第一,超过华为、BAT 总和 | 数人云宣布与UMCloud合并
- 河北钢铁的数字化雄心
- 上班15年后,普通程序员能实现财富自由吗?
- Delphi的高光时刻!C#之父Anders Hejlsberg的祝福:持续更新、持续发展的Delphi家族- Delphi 2009及其后的新功能
- 支持向量机回归预测SVR——MATLAB超详细代码实现过程
- Android传感器常见显示程序
- 04: 部署MongoDB服务 、 MongoDB基本使用
- excel取html文本长度,excel字符长度 怎么计算excel里的字符串的长度