从这节开始,开始使用开发工具来开发Python,因为交互式环境不方便编写代码,功能也不强大。
流程控制是为了控制代码的执行流程,好比人做事,要灵活。遇到不同的情况是否要执行,是否要重复执行,是否要批量执行(函数)。根据Python官网给出的流程控制知识点目录,函数也属于流程控制,但由于函数知识点庞大,留在下一章节详细讲解。
一、if 语句
最为人所知的就是 [if](https://docs.python.org/zh-cn/3.9/reference/compound_stmts.html#if) 语句。例如:
x = int(input("请输入一个整数: "))if x < 0:x = 0print('负数变为0')elif x == 0:print('Zero')elif x == 1:print('Single')else:print('More')
- if 语句包含零个或多个
[elif](https://docs.python.org/zh-cn/3.9/reference/compound_stmts.html#elif)子句,及可选的[else](https://docs.python.org/zh-cn/3.9/reference/compound_stmts.html#else)子句。关键字 ‘elif‘ 是 ‘else if’ 的缩写,适合用于避免过多的缩进。 If语句只会匹配一个表达式,一旦匹配上某一个,则结束匹配。
二、for 语句
遍历字符串
s = 'hello'for i in s:print(i)
遍历列表/元组
元组和列表是类似的数据结构,遍历方式一样,这里演示遍历列表皆可
eg1.
words = ['cat', 'window', 'defenestrate']for v in words:print(v, len(v))
输出:
cat 3
window 6
defenestrate 12
eg2.
words = ['cat', 'window', 'defenestrate']for v in enumerate(words):print(v, len(v))
输出:
(0, ‘cat’) 2
(1, ‘window’) 2
(2, ‘defenestrate’) 2
eg3.
words = ['cat', 'window', 'defenestrate']for k, v in enumerate(words):print(k,v, len(v))
输出:
0 cat 3
1 window 6
2 defenestrate 12
遍历字典
eg1.
d = {'a': 1, 'b': 2}for v in d:print(v)
eg2.
d = {'a': 1, 'b': 2}for v in d.values():print(v)
eg3.
d = {'a': 1, 'b': 2}for k, v in d.items():print(k, v)
遍历组合序列
L1 = ['a', 'b', 'c']L2 = ['d', 'e', 'f']for k, v in zip(L1, L2):print(k, v)
三、[range()](https://docs.python.org/zh-cn/3.9/library/stdtypes.html#range) 函数
eg1.
语法格式:class range(start, stop[, step])
for i in range(5):print(i)
eg2.
生成的序列不包含给定的终止数值;range(10) 生成 10 个值,这是一个长度为 10 的序列,其中的元素索引都是合法的。range 也能以另一个数字开头,或以指定的幅度递增(该幅度也可以是负数;称为 ‘步进’) :
range(5, 10)5, 6, 7, 8, 9range(0, 10, 3)0, 3, 6, 9range(-10, -100, -30)-10, -40, -70
[range()](https://docs.python.org/zh-cn/3.9/library/stdtypes.html#range) 和 [len()](https://docs.python.org/zh-cn/3.9/library/functions.html#len) 组合在一起,可以按索引遍历序列:
>>> a = ['Mary', 'had', 'a', 'little', 'lamb']>>> for i in range(len(a)):... print(i, a[i])...0 Mary1 had2 a3 little4 lamb
四、while语句
[while](https://docs.python.org/zh-cn/3.9/reference/compound_stmts.html#while) 语句用于在表达式保持为真的情况下重复地执行:
count = 0while (count < 5):print('The count is:', count)count = count + 1print("Good bye!")
输出:
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
Good bye!
五、多层循环
多个for、多个while、for+while等形式
for i in range(1, 3):for j in range(4, 6):print(i, j)
六、循环+判断
for+if
for i in range(1, 5):for j in range(2, 6):if i > j:print(i, j)
while+if
count = 0while (count < 5):print('The count is:', count)count = count + 1for b in range(5):if count + b > 8:print('count + b > 8,为: ', count + b)print("Good bye!")
输出:
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
count + b > 8,为: 9
Good bye!
七、break 和 continue
break
[break](https://docs.python.org/zh-cn/3.9/reference/simple_stmts.html#break) 语句和 C 中的类似,用于跳出最近的 [for](https://docs.python.org/zh-cn/3.9/reference/compound_stmts.html#for) 或 [while](https://docs.python.org/zh-cn/3.9/reference/compound_stmts.html#while) 循环。
for i in range(2, 6):print(i)if i > 3:break
输出:
2
3
4
for i in range(1, 5):for j in range(2, 6):if i > j:print(i, j)break
输出:
3 2
4 2
count = 0while True:print('The count is:', count)count = count + 1if count >= 5:breakprint("Good bye!")
输出:
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
Good bye!
continue
[continue](https://docs.python.org/zh-cn/3.9/reference/simple_stmts.html#continue) 在语法上只会出现于 [for](https://docs.python.org/zh-cn/3.9/reference/compound_stmts.html#for) 或 [while](https://docs.python.org/zh-cn/3.9/reference/compound_stmts.html#while) 循环所嵌套的代码中, 它会忽略本次循环[continue](https://docs.python.org/zh-cn/3.9/reference/simple_stmts.html#continue) 后面的代码,继续执行最近的外层循环的下一个轮次。
for num in range(2, 5):if num % 2 == 0:print("发现一个偶数", num)continueprint("发现一个奇数", num)
输出:
发现一个偶数 2
发现一个奇数 3
发现一个偶数 4
for i in range(2):for num in range(2, 5):if num % 2 == 0:print("发现一个偶数", i,num)continueprint("发现一个奇数", i,num)
输出:
发现一个偶数 0 2
发现一个奇数 0 3
发现一个偶数 0 4
发现一个偶数 1 2
发现一个奇数 1 3
发现一个偶数 1 4
八、pass 语句
[pass](https://docs.python.org/zh-cn/3.9/reference/simple_stmts.html#pass) 语句什么也不做。当语法上需要一个语句,但程序需要什么动作也不做时,可以使用它。例如:
>>> while True:... pass # 忙-等待键盘中断 (Ctrl+C)...
这通常用于创建最小的类:
>>> class MyEmptyClass:
… pass[pass](https://docs.python.org/zh-cn/3.9/reference/simple_stmts.html#pass) 的另一个可以使用的场合是在你编写新的代码时作为一个函数或条件子句体的占位符,允许你保持在更抽象的层次上进行思考。 pass 会被静默地忽略:
>>> def initlog(*args):... pass # Remember to implement this!...
