当前位置: 首页 > 图灵资讯 > 行业资讯> python常见循环结构有哪些

python常见循环结构有哪些

发布时间:2024-09-04 19:49:57

1、for…in…

这种格式是python中最常见的格式,应用广泛。

格式:for参数in循环体:
pass

在上述格式中,有许多内容可以用作循环体,如元组、列表、字符串等。只要能穿越循环,就可以作为循环体存在。

参数主要用于存储每个循环体发送的单个元素,从而实现循环功能。在实践中,它经常与if判断句相结合。

#input
str_01='时间去哪了!!!'
foriinstr_01:
print(i)

#output
时
间
都
去
哪
了
!
!
!

2、while

while循环和for...in..循环的区别在于,while应该首先初始化循环变量或直接使用while True 这种死循环形式。

格式:i=0
whilei>=10:
pass
i+=1
#input
whileTrue:
print('helloworld!!!__hellopython!!!')
#output
helloworld!!!__hellopython!!!
helloworld!!!__hellopython!!!
helloworld!!!__hellopython!!!
helloworld!!!__hellopython!!!
helloworld!!!__hellopython!!!
helloworld!!!__hellopython!!!
helloworld!!!__hellopython!!!
helloworld!!!__hellopython!!!
helloworld!!!__hellopython!!!
.
.
.
.
.
.

以上是python中两种常见的循环结构,希望对大家有所帮助。更多Python学习指导:python基础教程

相关文章

如何让vim支持python3

如何让vim支持python3

2025-09-12
python2.7和3.6区别有哪些

python2.7和3.6区别有哪些

2025-09-12
python3有serial库吗

python3有serial库吗

2025-09-12
python中w、r表示什么意思

python中w、r表示什么意思

2025-09-12
python中如何把list变成字符串

python中如何把list变成字符串

2025-09-12
python命名空间是什么

python命名空间是什么

2025-09-12