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

輸出如下:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章