python遍历列表

python遍历列表

遍历列表 :  输出所有元素

  1. 依次遍历列表
    效果图:
    python遍历列表_第1张图片

    代码:

    # 创建列表
    stus = ['孙悟空','猪八戒','沙和尚','唐僧','白骨精','蜘蛛精']
    
    # 依次遍历列表
    print(stus[0])
    print(stus[1])
    print(stus[2])
    print(stus[3])

     

  2. 通过while循环遍历列表
    效果图:

    代码:

    # 创建列表
    stus = ['孙悟空','猪八戒','沙和尚','唐僧','白骨精','蜘蛛精']
    # 通过while循环遍历列表
    i = 0
    while i < len(stus):
        print(stus[i])
        i += 1

     

  3. 通过for循环遍历列表    最好的遍历方法
    效果图:

    代码:

    # 创建列表
    stus = ['孙悟空','猪八戒','沙和尚','唐僧','白骨精','蜘蛛精']
    # 通过for循环遍历列表 
    for i in stus:
        print(i)

     


posted on 2019-07-14 16:50  人生与戏 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/FlyingLiao/p/11184749.html

你可能感兴趣的:(python遍历列表)