python2.7.2文档阅读笔记

虽然之前也写了几个小程序,但是还是心里没底,今天决定发发狠心,把官方文档看完,看看有啥收获没有,结果,哈哈,真实不错,以后在看其他版本的,下面记录了我看得笔记:(当然有很多特性大家可能已经知道好久了,我就当自己学习啦,我还是新手嘛~~)

1 print默认输出后会增加一个新的换行:例如print ‘a’ 结果是a\n(这个\n后来加的,被现实出来自动换行),使用pring xxx,可以去除自动换行。

2  for循环在python中的特点是:可以在任何序列数据结构上进行迭代(重复):例如字符串 和列表。并以元素在序列中出现的顺序迭代。

3 a[:]==a

4 一个函数的默认值只会被赋值一次:

 

def f(a, L=[]):
    L.append(a)
    return L

print f(1)
print f(2)
print f(3)

This will print

[1]
[1, 2]
[1, 2, 3]
5 声明函数的时候有默认值的参数必须声明在没有默认值的参数之后,在调用函数的时候keyword 参数必须在非keyword函数之后。
parrot()                     # required argument missing
parrot(voltage=5.0, 'dead')  # non-keyword argument after a keyword argument
parrot(110, voltage=220)     # duplicate value for the same argument
parrot(actor='John Cleese')  # unknown keyword argument
6 解除参数列表:
>>> range(3, 6)             # normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> range(*args)            # call with arguments unpacked from a list
[3, 4, 5]
>>> def parrot(voltage, state='a stiff', action='voom'):
...     print "-- This parrot wouldn't", action,
...     print "if you put", voltage, "volts through it.",
...     print "E's", state, "!"
...
>>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}
>>> parrot(**d)
-- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !
7 不像c语言python不能对字符串的某个item和片段进行赋值例如:
>>> word[0] = 'x'
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: object does not support item assignment
>>> word[:1] = 'Splat'
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: object does not support slice assignment
8 filter是一个函数式编程工具,这里filter(function,sequence)这时,如果这个输入的sequence是元组tuple或者string,那么filter返回的结果是相应类型,其他的情况一律返回list类型。
9 map则都是返回一个返回值的list,他们将函数应用在输入的序列上。可以接受多个序列作为输入,相应的函数也必须是相应个数目的参数。
10 reduce函数:
>>> def add(x,y): return x+y
...
>>> reduce(add, range(1, 11))
55
注意:如果输入序列是空的,那么一个异常将会被抛出。





posted @ 2012-03-04 20:50  dragon-in-the-sea  阅读(314)  评论(0)    收藏  举报