读书人

python 语法入门 二

发布时间: 2012-12-26 14:39:28 作者: rapoo

python 语法入门 2

python的数据类型有:

?? ?列表,元组,set,字典

?? ?列表有 append(n), insert(n,x),pop(n),remove(n),index(x),count(x),sort(),reverse();

?? ?还有列表内置函数,?

?? filter(func,[x,y]) ? func 返回true or false, 将列表中的值,分别放到func中根据返回的true 来将该值取出

?? map(func,[x,y]) 将列表中的值 放到func中,将返回值放到返回的列表中。

?? map(func,[x,y],[x,7])func中必须接受足够的参数,两个列表的长度必须一致

?? reduce(func,【2,3】,0) 第三个数可选

?? 计算前两个数,将返回值与第三个值进行计算。当有第三个参数时,将第三个参数和列表的第一个数进行计算,然后类推

del 删除列表中的值,或删除变量

?? del(a[2]),del(a[1:2]) ?del(:)删除全部元素, del(a),删除该变量

set()不重复的列表

?a, a - b, ? a & b a | b , a^b

?

循环语法: ?

?? ? for ?n in range:

?? ? ? ? ? ? ?print n ? ? ? ? ? ? ? ??

?? ? for k,v in dict.iteritems():

?? ? ? ? ? ? print k,v

?? ? for i,v in range.enumerate():

?? ? ? ? ? ? ? ?print i,v

for x,y in zip(list1,list2)

?? ? ? print x,y ? ? ? ? ? ? ?zip可以同时解读两个列表

sorted函数可以不改动原列表生成一个排好序的新列表

?

python的脚本需要以 .py结尾,?

?? ?在python的包中必须要有 __init__.py,可以有初始化操作,也可以为空

?from ?module import submodule?

?import module?

from module import *, 这样无法引入 __ 开始的定义

?dir()可以查看 模块的定义

?

?

读书人网 >perl python

热点推荐