"Python entry to proficiency" loop statement while loop, for loop

"Author's Homepage": Shibie Sanri wyx
"Author's Profile": CSDN top100, Alibaba Cloud Blog Expert, Huawei Cloud Share Expert, Network Security High-quality Creator
"Recommended Column": Xiaobai Zero Basic "Python Beginner to Master"

while is used to "repeatedly execute" a certain piece of code, enter the loop when the "condition is met" , and jump out of the loop when the "condition is not satisfied" .


1. Grammatical format

For different scenarios, while can use different "syntax formats"

1.1、while

The most "basic" grammatical structure, often used in simple scenarios.

while 判断条件:
	执行代码

Example: Define your balance, earn 1 yuan a day, earn enough 20 yuan, you can go to Internet cafes

money = 12

while money < 20:
    print(money, '元:穷逼,离开我的网吧')
    money += 1

print('这位爷,里边儿请')

output:

12 元:穷逼,离开我的网吧
13 元:穷逼,离开我的网吧
14 元:穷逼,离开我的网吧
15 元:穷逼,离开我的网吧
16 元:穷逼,离开我的网吧
17 元:穷逼,离开我的网吧
18 元:穷逼,离开我的网吧
19 元:穷逼,离开我的网吧
这位爷,里边儿请

1.2. Infinite loop

When the judgment condition is always True , the loop will "loop infinitely" . It is often used in some special scenarios, such as heartbeat requests between clients and servers.

while True:
	执行代码

Example: Define your balance, if you don’t earn money every day, if you earn enough 20 yuan, you can go to the Internet cafe.

money = 12

while money < 20:
    print(money, '元:穷逼,离开我的网吧')

print('这位爷,里边儿请')

output:

12 元:穷逼,离开我的网吧
12 元:穷逼,离开我的网吧
	......

1.3 Shorthand form

If the body of the while loop has only "one statement" , it can be written in one line.

money = 12

while money < 20: print(money, '元:穷逼,离开我的网吧')

2. continue skips the loop

The keyword continue can "skip a cycle" , without executing subsequent codes, and directly enter the next cycle.

Example: Define your balance, earn 1 yuan a day, earn enough 20 yuan, you can use the Internet cafe; but earn 15 yuan, you can take a day off.

money = 12

while money < 20:
    money += 1
    if money == 15:
        print('休息一下')
        continue
    print('余额:', money, '继续搬砖')

print('去网吧打游戏')

output:

余额: 13 继续搬砖
余额: 14 继续搬砖
休息一下
余额: 16 继续搬砖
余额: 17 继续搬砖
余额: 18 继续搬砖
余额: 19 继续搬砖
余额: 20 继续搬砖
去网吧打游戏

3. break ends the loop

The keyword break can "end" the entire loop

Example: Define your balance, earn 1 yuan a day, and if you earn 20 yuan, you will go to an Internet cafe; but when you earn 15 yuan, you will be exhausted.

money = 12

while money < 20:
    money += 1
    if money == 15:
        print('啊,我累死了')
        break
    print('余额:', money, '继续搬砖')

print('去网吧打游戏')

output:

余额: 13 继续搬砖
余额: 14 继续搬砖
啊,我累死了
去网吧打游戏

4. for loop

The for loop is often used to "traverse" iterable objects, such as traversing lists, tuples, etc.

Example: iterate through each element in the list

list1 = [1, 2, 3, 4]

for i in list1:
    print(i)

output:

1
2
3
4

5. pass placeholder

The role of the keyword pass is to "occupy space" , do nothing, maintain the integrity of the structure, and avoid "syntax errors" .

For example, as an empty execution body in a loop or a judgment statement:

for i in 'str':
    pass

money = 18
if money > 20:
    pass

Or in the early stage of development, only define the function structure, but do not realize the specific function, first use pass to occupy the place

def function1():
    pass

def funcation2():
    pass

Fan benefits

You can participate in the lottery by leaving a message in the comment area of ​​the article, and you will get 5 copies of "Interesting Computer Underlying Technology".

insert image description here

Students who are not selected can visit the linkhttps://item.m.jd.com/product/13987012.html?PTAG=17053.1.1&utm_source=weixin&utm_medium=weixin&utm_campaign=t_1000072672_17053_001

Guess you like

Origin blog.csdn.net/wangyuxiang946/article/details/131873025