python学习历程_语句(day 3)

来源:互联网 发布:电子地图一把抓软件 编辑:程序博客网 时间:2024/06/05 06:29

1、序列解包(递归解包):将多个值的序列解开,然后放到变量的序列中

vals=(1,2,3)   x,y,z = vals  >>x=1

2、布尔值

python中 False  None  0  " " ()   [ ]  { } 都看做假,也就是标准值False和None、所有类型数字0、空序列以及空的字典都为假。其他的一切都被解释为真(还可以用bool类型来转换)

3、assert 断言

>>> age = -1>>> assert age>0,'the age must be realistic'Traceback (most recent call last):  File "<pyshell#1>", line 1, in <module>    assert age>0,'the age must be realistic'AssertionError: the age must be realistic
4、zip函数  进行并行迭代,可以把两个序列压缩在一起,返回一个元组序列表,且zip函数可以处理不等长的序列,当最短序列用完的时候停止

>>> x = ['1','2','3','4']>>> y = ['a','b','c']>>> zip(x,y)<zip object at 0x028C1D00>               返回了一个可迭代对象>>> for m in zip(x,y):print(m)('1', 'a')('2', 'b')('3', 'c')
5、enumerate函数  可以在提供索引的地方迭代索引-值对
>>> seq = ['one', 'two', 'three']>>> for i , element in enumerate(seq):    if 'one' in seq:        seq[i] = 'l'    print(i,seq[i])    0 l1 two2 three
6、跳出循环 break、continue

7、while True/breake 语句(while True ......if......break)

8、列表推导式(列表解析)

>>> [x*x for x in range(10)][0, 1, 4, 9, 16, 25, 36, 49, 64, 81]>>> [x*x for x in range(10) if x%3==0][0, 9, 36, 81]>>> [(x,y) for x in range(3) for y in range(4)][(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]
9、pass,del ,exec、eval