读书笔记---Python绝对简明手册(个人认为Python是C++的最佳拍档)

 http://wiki.woodpecker.org.cn/moin/PyAbsolutelyZipManual
最新版
  • PyAbsolutelyZipManual

Python 绝对简明手册 -- [email protected] ::-- ZoomQuiet [2006-09-15 04:35:33]

CPUG联盟::

 

CPUG::门户

BPUG

SPUG

ZPUG

{ CPUGnav}

1. Py2.5 绝对简明手册

简述

zuroc主动在列表中分享

PYthon绝对简明手册,初学必备!

  • -- For Python2.5

版本:0.2 beta

作者:张沈鹏

参考:python2.5中文教程

感谢:Zoom.Quiet limodou

我的技术Blog 我的C++Blog 我的文学Blog

1.1. 语法

1.1.1. if

<script type="text/javascript"> function isnumbered(obj) { return obj.childNodes.length && obj.firstChild.childNodes.length && obj.firstChild.firstChild.className == 'LineNumber'; } function nformat(num,chrs,add) { var nlen = Math.max(0,chrs-(''+num).length), res = ''; while (nlen>0) { res += ' '; nlen-- } return res+num+add; } function addnumber(did, nstart, nstep) { var c = document.getElementById(did), l = c.firstChild, n = 1; if (!isnumbered(c)) if (typeof nstart == 'undefined') nstart = 1; if (typeof nstep == 'undefined') nstep = 1; n = nstart; while (l != null) { if (l.tagName == 'SPAN') { var s = document.createElement('SPAN'); s.className = 'LineNumber' s.appendChild(document.createTextNode(nformat(n,4,' '))); n += nstep; if (l.childNodes.length) l.insertBefore(s, l.firstChild) else l.appendChild(s) } l = l.nextSibling; } return false; } function remnumber(did) { var c = document.getElementById(did), l = c.firstChild; if (isnumbered(c)) while (l != null) { if (l.tagName == 'SPAN' && l.firstChild.className == 'LineNumber') l.removeChild(l.firstChild); l = l.nextSibling; } return false; } function togglenumber(did, nstart, nstep) { var c = document.getElementById(did); if (isnumbered(c)) { remnumber(did); } else { addnumber(did,nstart,nstep); } return false; } </script> <script type="text/javascript"> document.write(' Toggle line numbers<\/a>'); </script> Toggle line numbers
 1 x=int(raw_input("Please enter an integer:"))
2 if x<0:
3 print 'Negative Number'
4 elif x==0:
5 print 'Zero'
6 else:
7 print 'Positive Number'

1.1.2. for

<script type="text/javascript"> document.write(' Toggle line numbers<\/a>'); </script> Toggle line numbers
 1 a=['cat','door','example']
2 for x in a:
3 print x

#如果要修改a的内容,则用a的副本循环,如:

for x in a[:] : 
.....................

<script type="text/javascript"> document.write(' Toggle line numbers<\/a>'); </script> Toggle line numbers
 1 >>>range(10,0,-3)
2 [10,7,4,1]
3 a=['cat','door','example']
4 for i in range(len(a)):
5 print i,a[i]

break,continue 用法和C++中类似

1.1.3. pass

while True: 
pass #忽略,什么也不做

<script type="text/javascript"> document.write(' Toggle line numbers<\/a>'); </script> Toggle line numbers
 1 def fib(n=1000):#参数可以有默认值,多个可选参数赋值可以直接写"参数变量名=值"来快速赋值 
2 """这里给函数写文档注释"""
3 a,b=0,1
4 while b<n:
5 print b
6 a,b=b,a+b

#函数可以重命名,如

<script type="text/javascript"> document.write(' Toggle line numbers<\/a>'); </script> Toggle line numbers
 1 f=fib
2 f(223)

1.1.4. in

1.1.5. 参数格式 **para

#参数格式为 **para 表示接受一个字典,为 *para 表示接受一个元组

<script type="text/javascript"> document.write(' Toggle line numbers<\/a>'); </script> Toggle line numbers
 1 def test(para1,*args,**dic):
2 print para1
3 for arg in args : print arg
4 keys=dic.keys()
5 keys.sort()
6 for key in keys:print key ,':',dic[key]

1.1.6. Lambda函数

1.1.7. List的函数

append(x)    追加到链尾 
extend(L) 追加一个列表
insert(i,x) 在位置i插入x
remove(x) 删除第一个值为x的元素,如果不存在会抛出异常
pop([i]) 返回并删除位置为i的元素,i未给定时默认作用在最后一个元素.[i]表示i为可选的
index(x) 返回第一个值为x的元素,不存在则抛出异常
count(x) 返回x出现的次数
sort() 排序
reverse() 翻转,反转

filter(function函数 , sequence序列)  返回sequence中使filer为true的

map(function,sequence,[sequence...])  返回新的sequence,序列中值为对每个元素分别调用function.

reduce(function,sequence,[init])  返回一个单值为,计算步骤为 :

  • 第1个结果=function(sequence[0],sequence[1])
  • 第2个结果=function(第1个结果,sequence[2])
  • 返回最后一个计算得值
  • 如果有init,则先调用

function(init,sequence[0])  sequence只有一个元素时,返回该元素,为空时抛出异常.

1.1.8. 列表推导式

freshfruit=['  banana  ','   loganberry  '] 
>>>[weapon.strip() for weapon in freshfruit]
['banana','loganberry']
vec=[2,4,6]
>>>[3*x for x in vec if x>3]
[12,18]
>>>[(x,x**2) for x in vec] #一个元素一定要是一个sequence,而
[x,x**2 for x in vec]是错误的
[(2,4),(4,16),(6,36)]
vec2=[4,3,-9]
[x*y for x in vec for y in vec2]
[vec[i]+vec2[i] for i in range(len(vec))]
[str(round(355/113.0,i)) for i in range(1,6)] #str()是转换类型为可以打印的字符

1.1.9. del

<script type="text/javascript"> document.write(' Toggle line numbers<\/a>'); </script> Toggle line numbers
 1 a=[1,2,3,4,5,6]
2 del a[0]
3 >>>a
4 [2,3,4,5,6]
5 del a[2:4]
6 >>>a
7 [2,3,6]
8 del a[:]
9 >>>a
10 []
11 del a
12 >>>a

抛出异常

1.1.10. 元组

t=1234,5567,'hello' 
x,y,z=t #拆分操作可以应用于所有sequence
>>>x
1234
u=t,(1,2,3)
>>>u
((1234,5567,'hello'),(1,2,3))
empty=() #空元组
singleton='hi', #单个元素的元组

1.1.11. set

set(集合):无序不重复的元素集

<script type="text/javascript"> document.write(' Toggle line numbers<\/a>'); </script> Toggle line numbers
 1 basket = ['apple','orange','apple','pear','apple','banana']
2 fruit=set(basket)
3 >>>fruit
4 set(['orange', 'pear', 'apple', 'banana'])
5 >>>'orange' in fruit
6 True
7 a=set('abracadabew')
8 >>>a
9 set(['a', 'c', 'b', 'e', 'd', 'r', 'w'])
10 b=set('wajgwaoihwb')
11 >>> b
12 set(['a', 'b', 'g', 'i', 'h', 'j', 'o', 'w'])
13 >>> a-b #差
14 set(['c', 'r', 'e', 'd'])
15 >>> a|b #并
16 set(['a', 'c', 'b', 'e', 'd', 'g', 'i', 'h', 'j', 'o', 'r', 'w'])
17 >>> a&b #交
18 set(['a', 'b', 'w'])
19 >>>a^b #(并-交)
20 set(['c', 'e', 'd', 'g', 'i', 'h', 'j', 'o', 'r'])

=== dict ===

字典:关键字为不可变类型,如字符串,整数,只包含不可变对象的元组.列表等不可以作为关键字.如果列表中存在关键字对,可以用dict()直接构造字典.而这样的列表对通常是由列表推导式生成的.

<script type="text/javascript"> document.write(' Toggle line numbers<\/a>'); </script> Toggle line numbers
 1 tel={'jack':4098,'sape':4139}
2 tel['guido']=4127
3 >>> tel
4 {'sape': 4139, 'jack': 4098, 'guido': 4127}
5 >>>tel['jack']
6 4098
7 del tel['sape']
8 >>>tel.keys()
9 ['jack', 'guido']
10 >>>tel.has_key('jack')
11 True
12 knight={'gallahad':'the pure','robin':'the brave'}
13 for k,v in knight.iteritems():
14 print k,v

输出:

<script type="text/javascript"> document.write(' Toggle line numbers<\/a>'); </script> Toggle line numbers
 1 gallahad the pure
2 robin the brave

enumerate()返回索引位置和对应的值

<script type="text/javascript"> document.write(' Toggle line numbers<\/a>'); </script> Toggle line numbers
 1 for i,v in enumerate(['tic','tac','toe'])
2 print i,v

输出: 0 tic

1 tac

2 toe

1.1.12. zip

zip用于多个sequence的循环

<script type="text/javascript"> document.write(' Toggle line numbers<\/a>'); </script> Toggle line numbers
 1 questions=['name','quest','favorite color']
2 answers=['lancelot','the holy grail','blue']
3 for q,a in zip(questions,answers):
4 print 'What is your %s ? It is %s.'%(q,a)

输出:

What is your name ? It is lancelot. 
What is your quest ? It is the holy grail.
What is your favorite color ? It is blue.

1.1.13. reversed反向循环

<script type="text/javascript"> document.write(' Toggle line numbers<\/a>'); </script> Toggle line numbers
 1 for i in reversed(range(1,4)):
2 print i

输出:

3 
2

你可能感兴趣的:(C++,c,python,C#,读书)