天天看点

python3基础篇(五)——循环控制 python3基础篇(五)——循环控制 1 while循环控制2 for循环控制3 循环内的break和continue关键字

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

前言

1 阅读这篇文章我能学到什么?

  这篇文章将为你介绍python3的循环控制用法。在python3里循环控制有

for

while

两种。

——如果你觉得这是一篇不错的博文,希望你能给一个小小的赞,感谢您的支持。

目录

  • python3基础篇(五)——循环控制
  • 1 while循环控制
    • 1.1 while结构
    • 1.2 while——else结构
  • 2 for循环控制
    • 2.1 for结构
  • 3 循环内的break和continue关键字

1 while循环控制

1.1 while结构

语法结构:

while <condition>:
    <codeblock>
           

  循环开始前先判断是否满足

<condition>

条件,即条件表达式结果为布尔真,如果满足则执行

<codeblock>

,执行完后继续再判断是否满足

<condition>

,继续满足就继续执行

<codeblock>

直到条件不满足时循环结束,程序往后运行。

<codeblock>

代码块必须必

while

至少有一个空格或Table的缩进。

流程图:

代码示例:

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

运行结果:

4
3
2
1
0
           

  当

<condition>

恒为真时,循环为死循环会一直执行下去,比如

while(True)

1.2 while——else结构

语法结构:

while <condition>:
    <codeblock1>
else:
    <codeblock2>
           

  python3的

while

可以与

else

结合,实现当条件不满足时不执行循环体则执行

else

内代码块。

代码示例:

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")
           

运行结果:

4
3
2
1
0
else code block
else code block
           

  当循环条件为假时,将会实行

else

中的代码块。

2 for循环控制

2.1 for结构

语法结构:

for <variable> in <sequence>:
    <codeblock1>
else
    <codeblock2>
           

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

流程图:

代码示例

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

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

输出结果为:

0
1
2
----------------------
1
2
3
----------------------
1
2
3
----------------------
1
2
3
----------------------
1
2
3
----------------------
1
2
3
----------------------
           

3 循环内的break和continue关键字

  循环结构内可以使用关键字

break

continue

,break的作用的立即结束循环的执行,跳到循环外继续执行。coutinue则是立即结束循环的本次执行,跳到循环条件表达式处。

语法结构:

while <condition>:
    <codeblock1>
    break
    <codeblock2>
<codeblock3>
           

  当执行到

break

时表示该循环体的处理已经结束,不会执行

<codeblock2>

而是跳到

<codeblock3>

继续执行。

语法结构:

while <condition>:
    <codeblock1>
    continue
    <codeblock2>
<codeblock3>
           

  当执行到

continue

时表示循环本轮的处理已经结束,不会执行

<codeblock2>

而是跳到

<condition>

继续执行下一轮循环。

代码示例:

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")
           

运行结果:

4
3
End while
4
3
1
0
End for