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基础教程
