Python for循环语句详解:语法、示例及应用
Python中for语句用于循环遍历一个可迭代对象(如列表、元组、字符串等),语法如下:
for 变量 in 可迭代对象:
循环体
其中,变量表示每次循环中当前的元素,循环体表示每次循环需要执行的代码块。例如:
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
print(fruit)
输出结果为:
apple
banana
cherry
在每次循环中,变量fruit依次取遍了列表fruits中的所有元素。
原文地址: https://www.cveoy.top/t/topic/mZwg 著作权归作者所有。请勿转载和采集!