知行编程网知行编程网  2022-09-28 13:00 知行编程网 隐藏边栏  6 
文章评分 0 次,平均分 0.0
导语: 本文主要介绍了关于python常见循环结构有哪些的相关知识,包括python循环结构组成部分,以及python两种基本循环结构这些编程知识,希望对大家有参考作用。

python中常见的循环结构有哪些


1、for…in…

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

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

在上述格式中,有很多东西可以作为循环体,比如元组、列表、字符串等,只要能通过循环,就可以作为循环体存在。

参数主要用于存储每个循环体发送的单个元素,从而实现循环功能。在实践中,它通常与 if 语句一起使用。

#input
str_01 = '时间都去哪了!!!'
for i in str_01:
print(i)
 
#output
时
间
都
去
哪
了
!
!
!


2、while

while循环和for...in...循环的区别在于必须先初始化while循环变量或者直接使用while True的无限循环形式。

格式:i = 0
 while i >=10:
  pass
  i +=1
#input
while True:
print('hello world!!!__hello python!!!')
#output
hello world!!!__hello python!!!
hello world!!!__hello python!!!
hello world!!!__hello python!!!
hello world!!!__hello python!!!
hello world!!!__hello python!!!
hello world!!!__hello python!!!
hello world!!!__hello python!!!
hello world!!!__hello python!!!
hello world!!!__hello python!!!
.
.
.
.
.
.


以上是python中常见的两种循环结构,希望对你有所帮助。


更多Python学习指路:

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写
扫一扫二维码分享