Python-for and while loop -break, continue, pass, else instructions

Copyright: Without permission, please do not reprint without permission! ! ! https://blog.csdn.net/weixin_44755148/article/details/89954401

Loop (for and while)

for loop

Here Insert Picture Description

for i in [1,2,3,4,5]:
    print(i*5)
#答案如下:
5
10
15
20
25

#搭配range函数使用
for i in range(5):
    print(i)
#答案如下
0
1
2
3
4

Here Insert Picture Description

while loop

Here Insert Picture Description

a = 0
while a < 5:
    a = a + 1
    print(a)
#结果展示
1
2
3
4
5

#案例说明
password = ''  # 变量password用来保存输入的密码

while password != '123':
    password = input("请尝试输入密码")
print("欢迎回家")
#结果展示
请尝试输入密码256
请尝试输入密码123
欢迎回家

and while loops for comparison

1、例如要把字符串'猫看见'拆成一个个字符打印出来,这件事【工作量确定】,适合用for循环
2、比如输入密码,我们不知道它要几次才能输入正确,适合使用while循环

Cycle will be used four statements

break - if ... break cycle internal use only (if certain conditions are met, out of this cycle, the contents of the outer execution cycle)

Here Insert Picture Description
Here Insert Picture Description

continue (in line with the conditions of the end of this cycle, for the next cycle)

Here Insert Picture Description

pass (skip to occupy a position that "doing nothing")

Here Insert Picture Description

else (with the statement after the loop, the loop ends can be executed else statement)

Here Insert Picture DescriptionHere Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_44755148/article/details/89954401