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)

猜你喜欢

转载自www.cnblogs.com/FlyingLiao/p/11184749.html
今日推荐