方法:首先创建列表(“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 = 0 while i <p><strong>通过for循环遍历列表 最好的遍历方法</strong></p><p>效果图:<br></p><p><img src="https://img.php.cn/upload/image/682/404/734/1593335208319068.png?x-oss-process=image/resize,p_40" title="1593335208319068.png" alt="031bf20a08efb494f843d6fa62efd9b.png"></p><p>代码:</p><pre class="brush:php;toolbar:false"># 创建列表 stus = ['孙悟空','猪八戒','沙和尚','唐僧','白骨精','蜘蛛精'] # 通过for循环遍历列表 for i in stus: print(i)
推荐教程:《python教程》
Python免费学习笔记(深入):立即使用
在学习笔记中,你将探索 Python 的核心概念和高级技巧!











