第五章:条件循环

第五章:条件循环读书笔记

1.使用==运算符来判定两个对象是否相等,使用is判定两者是否等同。
2.endswith()函数的使用:

name.endswith('Gumby')  #name的值是否以Gumby结尾

3.startswit函数:
name.startswith('Gumby')#name的值是否以Gumby开头
4 x is y x和y是同一对象
x is not y x和y是不同对象
5.x in y x是y的成员
x in not y x不是y的成员
6.一个字母的顺序值可以用ord函数查到,ord函数和chr函数功能相反
7.三元运算符:
a if b else c 含义:如果b为真,返回a,负责返回a
8.name.strip() 去除字符串两侧的空格
name.isspace() 检查字符串是否有空格组成
9.items方法 d.items()将字典所有项以字典形式返回
10.并行迭代:zip函数可以把两个序列压缩在一起,然后返回一个元祖列表。

names=['anne','beth','george','damon']
ages=[12,45,32,102]
zip(names,ages)
[('anne', 12), ('beth', 45), ('george', 32), ('damon', 102)]

11.enumerate函数在提供索引的地方迭代索引-键值
12.while True/break语句:

while True:
    word=raw_input('Please enter a word:')
    if not word:break
    #处理word
    print 'The word was '+word

13.列表推导式–轻量级循环()利用其它列表创建新列表
[x*x for x in range(10)]

猜你喜欢

转载自blog.csdn.net/u011243684/article/details/51586512