zoukankan      html  css  js  c++  java
  • 11、Python流程控制之for循环

    一、for循环的语法与基本使用

    1.1 什么是for循环结构?

      循环结构就是重复执行某段代码块,除了while循环之外,还有for循环。

    1.2 为什么要用for循环结构?

      理论上for循环能做的事情,while循环都可以做。之所以要有for循环,是因为for循环在循环取值(遍历取值)比while循环更简洁。

    1.3 如何使用循环结构?

      python中有while与for两种循环机制,其中for循环称之为迭代循环,语法如下:

    for 变量名 in 可迭代对象: # 此时只需知道可迭代对象可以是字符串列表字典,我们之后会专门讲解可迭代对象
        代码一
        代码二
        ...
    
    例1
    for item in ['a','b','c']:
        print(item)
    # 运行结果
    a
    b
    c
    
    # 参照例1来介绍for循环的运行步骤
    # 步骤1:从列表['a','b','c']中读出第一个值赋值给item(item=‘a’),然后执行循环体代码
    # 步骤2:从列表['a','b','c']中读出第二个值赋值给item(item=‘b’),然后执行循环体代码
    # 步骤3: 重复以上过程直到列表中的值读尽
    

    二、for循环的应用

    2.1 while循环之循环取值:

    案例1:列表循环取值

    简单版
    l = ['alex_dsb', 'lxx_dsb', 'egon_nb']
    for x in l:  # x='lxx_dsb'
        print(x)
    
    复杂版:
    l = ['alex_dsb', 'lxx_dsb', 'egon_nb']
    i=0
    while i < 3:
        print(l[i])
        i+=1
    

    案例2:字典循环取值

    简单版
    dic={'k1':111,'k2':2222,'k3':333}
    for k in dic:
        print(k,dic[k])
    
    复杂版:while循环可以遍历字典,太麻烦了
    

    案例3:字符串循环取值

    msg="you can you up,no can no bb"
    for x in msg:
        print(x)
    

    2.2 总结for循环与while循环的异同:

      1、相同之处:都是循环,for循环可以干的事,while循环也可以做到。
    2、不同之处:while循环称之为条件循环,循环次数取决于条件何时变为假;for循环称之为"取值循环",循环次数取决in后包含的值的个数。

    for x in [1,2,3]:
        print('===>')
        print('8888')
    

    2.3 for循环控制循环次数:range():

      in后直接放一个数据类型来控制循环次数有局限性:当循环次数过多时,数据类型包含值的格式需要伴随着增加。

    for x in 'a c':
        inp_name=input('please input your name:   ')
        inp_pwd=input('please input your password:   ')
        # 字符串中有三个数据,循环了三次,若次数过多,则需要更多数据
    
    range函数可创建一个整数列表,一般用在 for 循环中。range()在python2中得到一个列表,在python3,不需要输出完整的列表,节省了空间。
    

    2.4 range补充知识:

      1、for搭配range,可以按照索引取值,但是麻烦,所以不推荐。

    l=['aaa','bbb','ccc'] # len(l)
    for i in range(len(l)):
        print(i,l[i])
    
    for x in l:
        print(l)
    
    2、range()在python3里得到的是一只"会下蛋的老母鸡"
    

    2.3 for+continue:

      案例:

    for i in range(6):  # 0 1 2 3 4 5
        if i == 4:
            continue
        print(i)
    
    

    2.4 for循环嵌套:

      外层循环循环一次,内层循环需要完整的循环完毕。
      PS:终止for循环只有break一种方案。

    for i in range(3):
        print('外层循环-->', i)
        for j in range(5):
            print('内层-->', j)
    

    三、补充:

      1、print的使用,python里面,print()函数默认换行,即默认参数end = ' '

    >>>print('hello %s' % 'egon')
    hello egon
    

      2、print之逗号的使用,相当于空格

    >>>print('hello','world','egon')
    hello world egon
    

      3、换行符:" "

    >>>print('hello
    ')
    hello
    
    
    >>>print('world')
    world
    

      4、print值end参数的使用
      设置print()函数的参数end=''(空字符串),从而实现不换行,也可以设置为其他字符进行输出。

    >>> print('hello
    ',end='')
    hello
    >>>
    
    >>> print('world')
    world
    >>>
    
    >>> print('hello',end='*')
    hello*>>>
    
    >>> print('world',end='*')
    world*>>>
    
  • 相关阅读:
    课后作业
    动手动脑
    原码,补码,反码
    CodingSouls团队项目冲刺-个人概况(7)
    《人月神话》阅读笔记03
    数据库学习
    家庭小账本——数据库的编写与测试
    家庭小账本——适配器的编写与测试
    UI高级组件
    UI高级组件
  • 原文地址:https://www.cnblogs.com/zuiyouyingde/p/12457644.html
Copyright © 2011-2022 走看看