方法:首先创建列表(“stus = ['孙悟空','猪八戒','蜘蛛精']”),然后通过for循环遍历列表即可(“for i in stus:print(i)”)。
遍历列表 : 输出所有元素
依次遍历列表
效果图:
代码:
# 创建列表stus = ['孙悟空','猪八戒','沙和尚','唐僧','白骨精','蜘蛛精']# 依次遍历列表print(stus[0])print(stus[1])print(stus[2])print(stus[3])
通过while循环遍历列表
效果图:
代码:
# 创建列表stus = ['孙悟空','猪八戒','沙和尚','唐僧','白骨精','蜘蛛精']# 通过while循环遍历列表i = 0while i < len(stus): print(stus[i]) i += 1
通过for循环遍历列表 最好的遍历方法
效果图:
代码:
# 创建列表stus = ['孙悟空','猪八戒','沙和尚','唐僧','白骨精','蜘蛛精']# 通过for循环遍历列表 for i in stus: print(i)
推荐教程:《python教程》
以上就是python如何遍历列表所有元素?的详细内容,更多请关注24课堂在线网其它相关文章!