• 技术文章 >Python技术 >Python基础教程

    python常见循环结构有哪些

    小妮浅浅小妮浅浅2021-02-25 15:54:54原创5666

    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中2种常见循环结构,希望能对大家有所帮助。更多Python学习指路:python基础教程

    专题推荐:python循环结构
    品易云
    上一篇:python中conftest如何使用? 下一篇:Python快捷代码片段的使用

    相关文章推荐

    • python中条件判断分为哪几类?• Python hash对象的属性有哪些• python中if嵌套命令如何理解?• python re.match和re.search的不同使用• python序列化与反序列化如何使用?• python中conftest如何使用?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网