Python学习(三)—— 循环、索引、迭代

好久没有学习了

例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!"

输出如下:

猜你喜欢

转载自blog.csdn.net/tangweiee/article/details/84862937
今日推荐