03-04程序结构
程序结构
程序结构定义就是语句和表达式按照什么样的顺序执行
顺序结构
python3的默认结构, 从上到下一行一行执行
分支结构
分支结构永远只有一个或者0个分支会被执行:在分支结构中,有了elif可以不用写else,所以才会有0个分支会被执行
条件只能够是bool类型或者可以隐式转换为bool类型
单分支
In [59]: a = 5
...: if a < 10:
...: print('a less than 10')
...: print(a)
...:
a less than 10
5
双分支
双分支结构里, 永远只有一个分支会被执行
In [60]: a = 5
...: if a < 10:
...: print('a less than 10')
...: else:
...: print('a not less than 10')
...: print(a)
...:
a less than 10
5
In [61]: a = 50
...: if a < 10:
...: print('a less than 10')
...: else:
...: print('a not less than 10')
...: print(a)
...:
a not less than 10
50
多分支
通过解构嵌套可以实现多分支, 但是不建议
建议用elif语法来进行多分支
In [62]: a = 50
...: if a < 10:
...: print('a < 10')
...: else:
...: if a < 20:
...: print('10 <= a < 20')
...: else:
...: print('a >= 20')
...:
a >= 20
可以通过嵌套可以实现多分支
上述例子另一种写法
In [63]: a = 50
...: if a < 10:
...: print('a < 10')
...: elif a < 20:
...: print('10 <= a < 20')
...: else:
...: print('a >=20')
...:
a >=20
In [48]: if a < 10:
...: print('a < 10')
...: elif a < 20: # 有了elif可以不写else
...: print('10 <= 10 < 20')
...:
循环结构
while循环(死循环)
条件为真时死循环执行,条件为假时,结束
In [65]: i = 0
...: while i < 10:
...: print(i)
...: i += 1
...:
0
1
2
3
4
5
6
7
8
9
一定要有某种机制修改调制使其退出循环, 通常是在循环体里修改条件
for in 循环
文字描述
开始
for 元素 in 可迭代对象:
操作
结束
for in 循环里永远不要修改可迭代对象,被循环的可迭代对象会报错
In [70]: for i in range(0, 10):
...: print(i)
0
1
2
3
4
5
6
7
8
9
break终止与continue跳过
提前终止:break,这个循环终止了,已经退出了当前for或者while循环。
跳过::continue,跳过当前循环,处于continue后面的语句都不会再执行。
break和continue只针对最近的一层
break和continue只能用在循环里
例21:提前终止
In [71]: for i in range(0, 10):
...: if i % 2 != 0:
...: print(i)
...: break # 遇到break这个循环就全部终止了
...:
1
In [72]: for i in range(0, 10):
...: print(i)
...: if i % 2 != 0:
...: print('find it')
...: break
...:
0
1
find it
例22:跳过
In [73]: for i in range(0, 10):
...: print(i)
...: if i % 2 != 0:
...: continue
...: print(i + 1)
...:
0
1
1
2
3
3
4
5
5
6
7
7
8
9
9
In [74]: for i in range(0, 10):
...: print(i)
...: if i % 2 != 0:
...: continue
...: print('i is {}, i+1 is {}'.format(i, i + 1)) # 会将后面的循环体全部跳过
...:
0
i is 0, i+1 is 1
1 # 1对2取模不等于0, 返回为True, 就执行continue跳过, 那么后面的print也跳过了,所以后续直接显示2
2
i is 2, i+1 is 3
3
4
i is 4, i+1 is 5
5
6
i is 6, i+1 is 7
7
8
i is 8, i+1 is 9
9
continue用于跳过循环体剩下的部分
In [38]: for i in range(0, 10): # 和上个示例对比, 没有continue的部分
...: print(i)
...: if i % 2 != 0:
...: print('if % 2 is {}'.format(i))
...: print('i is {}, i+1 is {}'.format(i, i + 1))
...:
0
i is 0, i+1 is 1
1
if % 2 is 1
i is 1, i+1 is 2
2
i is 2, i+1 is 3
3
if % 2 is 3
i is 3, i+1 is 4
4
i is 4, i+1 is 5
5
if % 2 is 5
i is 5, i+1 is 6
6
i is 6, i+1 is 7
7
if % 2 is 7
i is 7, i+1 is 8
8
i is 8, i+1 is 9
9
if % 2 is 9
i is 9, i+1 is 10
例23:多层循环的break
In [75]: for i in range(0, 5):
...: for j in range(0, 10):
...: if j > 5:
...: break
...: print('i is {}, j is {}'.format(i, j))
...:
i is 0, j is 0
i is 0, j is 1
i is 0, j is 2
i is 0, j is 3
i is 0, j is 4
i is 0, j is 5 # 只有内部的for循环终止, 外部的for循环并不终止
...
i is 4, j is 4
i is 4, j is 5
In [76]: for i in range(0, 5):
...: for j in range(0, 10):
...: if j != 5:
...: continue
...: print('i is {}, j is {}'.format(i, j))
...:
i is 0, j is 5
i is 1, j is 5
i is 2, j is 5
i is 3, j is 5
i is 4, j is 5
break和continue只针对最近的一层
break和continue只能用在循环里
else子句
在python中,不管是if还是for还有while循环都有else语句。
当循环没有提前退出时, 都会执行else字句(也就是说,如果提前退出就不会执行else语句)
In [42]: for x in range(0, 5):
...: print(x)
...: break # 因为是终止,所以else都终止了
...: else:
...: print('ok')
...:
0
In [41]: for x in range(0, 5):
...: print(x) # continue是仅仅针对这个循环, 所以else会有效
...: continue
...: else:
...: print('ok')
...:
0
1
2
3
4
ok
In [43]: for x in range(0, 5):
...: print(x)
...: else:
...: print('ok') # for执行完后依然会执行else语句
...:
0
1
2
3
4
ok
In [46]: a = 1
...: while a == 1:
...: print(a)
...: a+=1
...: else: # while循环后依然会执行else语句
...: print('2')
...:
1
2