Python3 basics (five)-loop control

Python3 basics (five)-loop control

Foreword
1 What can I learn from reading this article?
  This article will introduce you to the usage of loop control in python3. In python3 where there are cycle control forand whiletwo kinds.

——If you think this is a good blog post, I hope you can give a small praise, thank you for your support.

1 while loop control

1.1 while structure

Grammatical structures:

while <condition>:
    <codeblock>

  Before the start of the loop, determine whether the <condition>condition is satisfied , that is, the result of the conditional expression is Boolean true. If it is satisfied, it will be executed <codeblock>. After the execution is completed, continue to judge whether it is satisfied <condition>. Continue to meet and continue to execute <codeblock>until the condition is not satisfied. <codeblock>The code block must have whileat least one space or Table indentation.
flow chart:

Created with Raphaël 2.2.0 开始 <condition> <codeblock> 结束 yes no

Code example:

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

operation result:

4
3
2
1
0

  When the <condition>constant is true, the loop will continue to execute forever, for example while(True).

1.2 while——else结构

Grammatical structures:

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

  Python3 whilecan be elsecombined with the implementation of the elseinner code block if the loop body is not executed when the condition is not met .
Code example:

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

operation result:

4
3
2
1
0
else code block
else code block

  When the loop condition is false, the elsecurrent code block will be executed .

2 for loop control

2.1 for structure

Grammatical structures:

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

  Python3's for loop is used to traverse the elements within a range of sequences. This sequence can be a range of values, strings, tuples, lists, sets, dictionaries. Using it flexibly will allow you to manipulate these data types more freely.
flow chart:

Created with Raphaël 2.2.0 开始 <sequence>序列中还有未遍历的元素? <variable>指向下一个元素 <codeblock> 结束 yes no

Code example

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关键字

  循环结构内可以使用关键字breakcontinue,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
发布了31 篇原创文章 · 获赞 95 · 访问量 1万+

Guess you like

Origin blog.csdn.net/qq_42475711/article/details/105467675