Python basics loop statements

Release: 2023-07-25 15:13:20
forward
660 people have browsed it

1. Scene introduction

##<1> Circular scenes in life

Runway


Python basics loop statements##Fan

Python basics loop statements##<2> 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遍)...
Copy after login

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

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 ...(省略)...
Copy after login

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

运行结果 :

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

<2> 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)
Copy after login

运行结果:

Python basics loop statements


<3> while循环嵌套

while嵌套的格式 :

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

例:九九乘法表

代码如下:

i = 1 while i<=9: j=1 while j<=i: print("%d*%d=%-2d "%(j,i,i*j),end='') j+=1 print('\n') i+=1
Copy after login

运行结果:

Python basics loop statements

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


for

<1> for循环介绍

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

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

<2> for循环的格式

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

<3> 小项目

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

name = 'dongGe' for x in name: print(x)
Copy after login

运行结果:

d o n g G e
Copy after login


总结:

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

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

break

<1> break和for嵌套

带有break的循环示例如下:

name = 'dongGe' for x in name: print('----') if x == 'g': break print(x)
Copy after login

运行结果:

Python basics loop statements


<2> break和while循环嵌套

带有break的循环示例如下:

i = 0 while i<10: i = i+1 print('----') if i==5: break print(i)
Copy after login

Python basics loop statements


小总结 :

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


Continue

<1> continue和for嵌套

带有continue的循环示例如下 :

name = 'dongGe' for x in name: print('----') if x == 'g': continue print(x)
Copy after login

运行结果:

Python basics loop statements


<2>continue和 while嵌套

带有continue的循环示例如下 :

i = 0 while i<10: i = i+1 print('----') if i==5: continue print(i)
Copy after login

运行结果 :

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!

Related labels:
source:Go语言进阶学习
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!