Python 循环迭代器
循环迭代器
我们还可以使用 for
循环来遍历可迭代对象:
例子 1
迭代元组的值:
mytuple = ("apple", "banana", "cherry") for x in mytuple: print(x)
例子 2
迭代字符串的字符:
mystr = "banana" for x in mystr: print(x)
for
循环实际上会创建一个迭代器对象,并为每个循环执行 next()
方法。
我们还可以使用 for
循环来遍历可迭代对象:
迭代元组的值:
mytuple = ("apple", "banana", "cherry") for x in mytuple: print(x)
迭代字符串的字符:
mystr = "banana" for x in mystr: print(x)
for
循环实际上会创建一个迭代器对象,并为每个循环执行 next()
方法。