遍历列表,指将列表中的所有元素取出来
创建一个列表
stus = [ “孙悟空”, “猪八戒”,“沙和尚”,"唐僧"]
遍历列表
print(stus[0])
print(stus[1])
print(stus[2])
print(stus[3])
通过while循环来遍历列表
i = 0
while i < 4 : #索引数不能写死,需要用函数写
print(stus[ i])
i +=1
i = 0
while i < len(stus) : #索引数不能写死,需要用函数写
print(stus[ i])
i +=1
通过for 循环来遍历列表
语法:
for 变量 in 序列:
代码块
for循环的代码块会执行多次,序列中有几个元素就会执行几次
每执行一次就会将序列中的一个元素赋值 给变量
所以我们可以通过变量,来攻取列表中的元素
for s in stus :
print(s)
网友评论