Home  >  Article  >  Backend Development  >  Python basics loop statements

Python basics loop statements

Go语言进阶学习
Go语言进阶学习forward
2023-07-25 15:13:20674browse

1. Scene introduction

##f35d6e602fd7d0f0edfa6f7d103c1b57 Circular scenes in life

Runway


Python basics loop statements##Fan

Python basics loop statements##2cc198a1d5eb0d3eb508d858c9f5cbdb Usage scenarios of loops in software development

When admitting a mistake, saying "I was wrong" ten thousand times will feel very troublesome and cumbersome.

    print("我错了")
    print("我错了")
    print("我错了")
    ...(还有9997遍)...

Use a loop statement to do it in one sentence.
    i = 0
    while i<10000:
        print("我错了")
        i+=1

Generally, code that needs to be executed multiple times can be completed in a loop.
Loops are not necessary, but in order to improve the reuse rate of code, experienced developers will use loops.

2. Introduction to common loops (while, for, break and continue)

while

##< ;1> Format of while loop

 while 条件:
        条件满足时,做的事情1
        条件满足时,做的事情2
        条件满足时,做的事情3
        ...(省略)...

Example:
    i = 0
    while i<5:
        print("当前是第%d次执行循环"%(i+1))
        print("i=%d"%i)
        i+=1

运行结果 :

    当前是第1次执行循环
    i=0
    当前是第2次执行循环
    i=1
    当前是第3次执行循环
    i=2
    当前是第4次执行循环
    i=3
    当前是第5次执行循环
    i=4

2cc198a1d5eb0d3eb508d858c9f5cbdb while循环应用

例:计算1~100的累积和(包含1和100)

#encoding=utf-8


i = 1
sum = 0
while i<=100:
    sum = sum + i
    i += 1


print("1~100的累积和为:%d"%sum)

运行结果:

Python basics loop statements


5bdf4c78156c7953567bb5a0aef2fc53 while循环嵌套

 while嵌套的格式 :

while 条件1:


    条件1满足时,做的事情1
    条件1满足时,做的事情2
    条件1满足时,做的事情3
    ...(省略)...


    while 条件2:
        条件2满足时,做的事情1
        条件2满足时,做的事情2
        条件2满足时,做的事情3
        ...(省略)...

例:九九乘法表

代码如下:

i = 1
while i<=9:
    j=1
    while j<=i:
        print("%d*%d=%-2d "%(j,i,i*j),end=&#39;&#39;)
        j+=1
        print(&#39;\n&#39;)
        i+=1

运行结果:

Python basics loop statements

可以看到while循环可以轻松的实现,避免出现代码冗余的情况。


for

f35d6e602fd7d0f0edfa6f7d103c1b57 for循环介绍

像while循环一样,for可以完成循环的功能。

在Python中for循环可以遍历任何序列的项目,如一个列表或者一个字符串等。

2cc198a1d5eb0d3eb508d858c9f5cbdb for循环的格式

for 临时变量 in 列表或者字符串等:
    循环满足条件时执行的代码

5bdf4c78156c7953567bb5a0aef2fc53 小项目

定义name变量,for循环打出以下结果。

name = &#39;dongGe&#39;


for x in name:
    print(x)

运行结果:

d
o
n
g
G
e


总结:

1.while循环一般通过数值是否满足来确定循环的条件。

2.for循环一般是对能保存多个数据的变量,进行遍历。

break

f35d6e602fd7d0f0edfa6f7d103c1b57 break和for嵌套

带有break的循环示例如下:

name = &#39;dongGe&#39;


for x in name:
    print(&#39;----&#39;)
    if x == &#39;g&#39;: 
        break
    print(x)

运行结果:

Python basics loop statements


2cc198a1d5eb0d3eb508d858c9f5cbdb break和while循环嵌套

带有break的循环示例如下:

i = 0


while i<10:
    i = i+1
    print(&#39;----&#39;)
    if i==5:
        break
    print(i)

Python basics loop statements


小总结 :

break的作用:用来结束整个循环。


Continue

f35d6e602fd7d0f0edfa6f7d103c1b57 continue和for嵌套

带有continue的循环示例如下 :

name = &#39;dongGe&#39;


for x in name:
    print(&#39;----&#39;)
    if x == &#39;g&#39;: 
        continue
    print(x)

运行结果:

Python basics loop statements


2cc198a1d5eb0d3eb508d858c9f5cbdb  continue和 while嵌套

带有continue的循环示例如下 :

i = 0


while i<10:
    i = i+1
    print(&#39;----&#39;)
    if i==5:
        continue
    print(i)

运行结果 :

Python basics loop statements

小总结 :

1. continue的作用:用来结束本次循环,紧接着执行下一次的循环。

2. break/continue只能用在循环中,除此以外不能单独使用。

3. break/continue在嵌套循环中,只对最近的一层循环起作用。

三、总结

本文以生活中的基础现象为切入点,主要介绍了Python基础中循环语句,对于每个循环的用法,以及循环之间相互嵌套使用,做了详细的讲解,用丰富的案例帮助大家更好理解。

The above is the detailed content of Python basics loop statements. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:Go语言进阶学习. If there is any infringement, please contact admin@php.cn delete