好久没有学习了
例1.循环遍历
for letter in 'Python': # 第一个实例
print '当前字母 :', letter
fruits = ['banana', 'apple', 'mango']
for fruit in fruits: # 第二个实例
print '当前水果 :', fruit
print "Good bye!"
输出结果如下:

例2.序列索引迭代
len()和range(),len()返回列表的长度,range()返回一个序列的数。
ws = ['hello', 'world', 'bye']
for index in range(len(ws)):
print '当前输出 :', ws[index]
print "Good bye!"
输出如下:

本文介绍了Python中使用for循环遍历字符串和列表的基本方法,以及如何利用len()和range()进行序列索引迭代,是Python初学者理解循环结构和序列操作的实用指南。
641

被折叠的 条评论
为什么被折叠?



