Python3基础篇(五)——循环控制

前言:
阅读这篇文章我能学到什么?
  这篇文章将为你介绍Python3的循环控制用法。在Python3里循环控制有forwhile两种。

1 while循环控制

1.1 while结构

语法结构:

1
2
while <condition>:
<codeblock>

  循环开始前先判断是否满足<condition>条件,即条件表达式结果为布尔真,如果满足则执行<codeblock>,执行完后继续再判断是否满足<condition>,继续满足就继续执行<codeblock>直到条件不满足时循环结束,程序往后运行。<codeblock>代码块必须必while至少有一个空格或Table的缩进。
流程图:

1
2
3
4
5
6
7
8
9
10
flowchat
Start=>start: 开始
End=>end: 结束
Condition=>condition: <condition>
Operation=>operation: <codeblock>

Start->Condition
Condition(yes, right)->Operation
Condition(no, bottom)->End
Operation(top)->Condition

代码示例:

1
2
3
4
a = 5
while(a > 0): #循环边界条件
a -= 1 #循环执行内容
print(a) #循环执行内容

运行结果:

1
2
3
4
5
4
3
2
1
0

  当<condition>恒为真时,循环为死循环会一直执行下去,比如while(True)

1.2 while——else结构

语法结构:

1
2
3
4
while <condition>:
<codeblock1>
else:
<codeblock2>

  Python3的while可以与else结合,实现当条件不满足时不执行循环体则执行else内代码块。
代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
a = 5
while(a > 0):
a -= 1
print(a)
else: #当a=0时不满足a > 0所以执行else中代码块
print("else code block")

a = -1
while(a > 0): #初始a=-1不满足a > 0所以执行else中代码块
a -= 1
print(a)
else:
print("else code block")

运行结果:

1
2
3
4
5
6
7
4
3
2
1
0
else code block
else code block

  当循环条件为假时,将会实行else中的代码块。

2 for循环控制

2.1 for结构

语法结构:

1
2
3
4
for <variable> in <sequence>:
<codeblock1>
else
<codeblock2>

  Python3的for循环被用于遍历一个序列范围内的元素,这个序列可以是数值范围、字符串、元组、列表、集合、字典。灵活运用它将会是你更自如的操作这些数据类型。
流程图:

1
2
3
4
5
6
7
8
9
10
11
12
flowchat
Start=>start: 开始
End=>end: 结束
Condition=>condition: <sequence>序列中还有未遍历的元素?
Operation1=>operation: <variable>指向下一个元素
Operation2=>operation: <codeblock>

Start->Condition
Condition(yes, right)->Operation1
Operation1(right)->Operation2
Operation2(top)->Condition
Condition(no, bottom)->End

代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
number = 3
for val in range(number): #遍历整数元素,range函数限制了范围为整数从0~3,步长为1
print(val)
print("----------------------")

string = "123"
for val in string: #遍历字符串中的字符
print(val)
print("----------------------")

Tuple = (1, 2, 3)
for val in Tuple: #遍历元组元素
print(val)
print("----------------------")

List = (1, 2, 3)
for val in List: #遍历列表元素
print(val)
print("----------------------")

Set = {1, 2, 3}
for val in Set: #遍历集合元素
print(val)
print("----------------------")

Dictionary = {"1":1, "2":2, "3":3}
for val in Set: #遍历字典元素
print(val)
print("----------------------")

  val是循环体内的局部变量,变量名可以自己定义。它的会依次遍历序列里的每一个元素。
输出结果为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
0
1
2
----------------------
1
2
3
----------------------
1
2
3
----------------------
1
2
3
----------------------
1
2
3
----------------------
1
2
3
----------------------

3 循环内的break和continue关键字

  循环结构内可以使用关键字breakcontinue,break的作用的立即结束循环的执行,跳到循环外继续执行。coutinue则是立即结束循环的本次执行,跳到循环条件表达式处。
语法结构:

1
2
3
4
5
while <condition>:
<codeblock1>
break
<codeblock2>
<codeblock3>

  当执行到break时表示该循环体的处理已经结束,不会执行<codeblock2>而是跳到<codeblock3>继续执行。
语法结构:

1
2
3
4
5
while <condition>:
<codeblock1>
continue
<codeblock2>
<codeblock3>

  当执行到continue时表示循环本轮的处理已经结束,不会执行<codeblock2>而是跳到<condition>继续执行下一轮循环。
代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
a = 5
while a > 0:
a -= 1
if a == 2: #轮循到a为2时即结束循环
break
print(a)
print("End while")

a = 5
while a > 0:
a -= 1
if a == 2: #轮询到a为2时结束本轮循环
continue
print(a)
print("End for")

运行结果:

1
2
3
4
5
6
7
8
4
3
End while
4
3
1
0
End for