Python中控制逻辑语句

if条件语句

1
2
3
4
5
6
if expr_1:
statement
elif expr_2:
statement
else:
statement

备注,if语句也可以组成Python的三元表达式,如下:

1
z = x if expr else y

如上语句表示如果expr为True,三元表达式返回x,否则返回y。有点类似C/C++语言中bool?a:b表达式,但Python中并没有问号表达式。

while循环语句

1
2
while expr:
statement

for循环语句

1
2
for word in words:
statement

使用举例

(1)遍历列表

1
2
3
4
5
6
7
8
9
10
citys = ['beijing', 'shanghai', 'chongqin', 'tianjin']

for city in citys:
print city

for i in range(len(citys)):
print citys[i]

for i, num in enumerate(nums):
print i, num

(2)遍历字典

1
2
3
4
5
6
dict_citys = {'beijing': 1, 'tianjin': 2, 'chongqin': 3, 'shanghai': 4} 
for key in dict_citys:
print key, 'corresponds to', dict_citys[key]

for key, value in dict_citys.items():
print key, 'corresponds to', dict_citys[key]