假值表達式: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