假值表达式:False None 0 "" () [] {}
if语句:
if 条件:
语句
elif 条件:
else:
is 运算符,判定同一性而不是相等性,如下:
- >>> x=y=[1,2,3]
- >>> z=[1,2,3]
- >>> x == y
- True
- >>> x == z
- True
- >>> x is y
- True
- >>> x is z
- False
布尔运算:
and : 两个条件都为值的时候整个表达式为真
or : 两个条件都为假的时候整个表达式为假
not:真为假,假为真,反过来的
while循环格式:
while 条件:
for循环格式:
for 变量 in 列表:
for 遍历字典元素:
for key in d:
print key,d[key]
for key,value in d.items():
print key,value
zip并行迭代
- names = ['anne','beth','george','damon']
- ages = [12,42,32,102]
- for name,age in zip(names,ages):
- print name,'is',age,'years old'
- 结果:
- anne is 12 years old
- beth is 42 years old
- george is 32 years old
- damon is 102 years old