python for循环遍历列表和元组

作者: python 发布时间: 2022-10-22 浏览: 573 次 编辑

python for循环遍历列表和元组

当用 for 循环遍历 list 列表或者 tuple 元组时,其迭代变量会先后被赋值为列表或元组中的每个元素并执行一次循环体。

下面程序使用 for 循环对列表进行了遍历:

my_list = [1,2,3,4,5]
for ele in my_list:
    print('ele =', ele)

程序执行结果为:

ele = 1
ele = 2
ele = 3
ele = 4
ele = 5