Advanced usage of traversing loops (so simple...)

1. Traverse the loop for
2. Infinite loop while
3. Loop control reserved word break continue
4. Advanced usage of loop else

1. Traverse the loop

从遍历结构中逐一提取元素,放在循环变量中
由保留字 forin 组成,完整遍历所有元素后结束
每次循环,所获得元素放入循环变量,并执行一次语句块
for <循环变量> in <遍历结构>:
    <语句块>

1.1 Counting loop

for i in range(3):
    print(i,end="")  

Detailed explanation

end="" 的意思为不换行 
end="," 意思为每个打印末尾添加,,不换行  
print()函数默认自动换行

Output

 0 1 2

1.2 Counting loop

for i in range(1,5):
    print(i,end="")

Output

 1 2 3 4

1.3 Counting loop

for i in range(1,5,2):
    print(i)

Output

1 3

Detailed explanation

第三个参数为<步长> 每隔多少个的意思

1.4 String traversal loop

Traverse the loop as many times as there are strings

for i in <字符串>:
    <语句块>
num = 0
for i in "abc":
    print(i,end="")
    num = num +1
    print(num,end="")

The output result is

a 1 b 2 c 3

1.5 List traversal loop

for item in ls:
    <语句块>

ls is a list, 遍历其每个元素put each element in a itemvariable, generate a loop
***这里的item变量也可以是i只是一个变量名字与命名无关

ls =[123,"py",456]
num1 = 0
for i in ls:
    print(i,end="<-列表里面的元素")
    num1 = num1+1
    print(num1) 

Output result

123<-列表里面的元素1
py<-列表里面的元素2
456<-列表里面的元素3

1.6 File traversal loop

for line in file:
    <语句块>
file = open('book3.txt','rb')
# 得到的数据类型为<class '_io.BufferedReader'>

Detailed explanation

# 打开文件 book3.txt 读取方式为 rb 并且命名为file               
# 因为本py脚本文件与book3.txt文件处于同一个文件目录下,所以不用添加指定盘符路径
# 采用rb模式打开文本,因为文本里面存在中文,目的避免出现乱码
# 注意 open()函数里面的参数都需要添加引号

To prevent errors, you can use the readlines()function

file1 = file.readlines()

readlines()Function means to read all the lines in the file into the variable file1 and automatically convert it to the list mode. <class 'list'>
Iterate as many times
lineDecode = line.decode("UTF-8")as there are lines , which means to read and decode the data in the variable in UTF-8 format. Because there are Chinese in my file, otherwise it will report an error when the original binary file is displayed

file = open('book3.txt','rb')
num2 = 0
for line in file:
    lineDecode= line.decode("UTF-8")
    num2 = num2 +1
    print(lineDecode)
    print(num2)

2 infinite loop

2.1 Cyclic operation mode controlled by conditions

while < 条件 > :
    <语句块>
    担负执行语句块,直到条件不满足时结束

2.2 Conditions for an infinite loop

The following code is executed only three times

a1 = 3
while a1>0:
    a1=a1-1
    print(a1)

The following code is executed indefinitely

a2 = 3
while a2 >0:
    a2 = a2 +1
    print(a2)

pythonPress in the environment to Ctrl+cexit the loop (you can also close cmd)

3 loop control reserved word break continue

break jumps out and ends the current entire loop, executes the statement
continue after the loop, ends the current loop, and continues to execute the subsequent cycles

a3 = 10
for i in "Luichun":
    if i =="c":
        continue
    a3 = a3 + 1
    print(i,a3)

The above code means: when the string is equal to c, ignore the current loop and go directly to the next loop

a4 = 10
for i in "Luichun":
    if i =="c":
        break
    print(i)

The above code means; when the string is equal to c, the rest of the loop is stopped, and the whole loop is ended
当有多个循环break怎么执行?(这个循环为break最内层的循环)不对外层影响

Advanced usage of 4 loops

4.1 Loop and Else

When the loop is not breakexited by a statement, the elsestatement block is executed
elseas a reward for completing the loop normally

for i in "dalao":
    if i =="a":
        continue
    print(i,end="")
else:
    print("正常退出")

The output result is

dlo正常退出
for i in "dalao":
    if i =="a":
        break
    print(i,end="")
else:
    print("正常退出")

The output result is

d

Because when the loop is areached, the condition triggers the rest and exits directly, and the elsestatement block is not executed.

Guess you like

Origin blog.csdn.net/weixin_47021806/article/details/114702433