人生与戏

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

遍历列表 :  输出所有元素

  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  人生与戏  阅读(37384)  评论(0编辑  收藏  举报