開始使用免費開始

疊代可疊代物件(1)

很好,現在你已經熟悉可疊代物件與疊代器是什麼了!在這個練習中,你會透過對可疊代物件與疊代器進行疊代並列印內容,來加強你的理解。

我們提供了一個字串清單 flash。你會先用 for 迴圈練習如何走訪清單。你也會為這個清單建立一個疊代器,並從疊代器中取出值。

本練習屬於課程

Python 工具箱

檢視課程

練習說明

  • 建立一個 for 迴圈來走訪 flash,並列印清單中的值。請使用 person 作為迴圈變數。
  • 為清單 flash 建立一個疊代器,並將結果指定給 superhero
  • 使用 next() 連續 4 次,分別列印 superhero 中的每個項目。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Create a list of strings: flash
flash = ['jay garrick', 'barry allen', 'wally west', 'bart allen']

# Print each list item in flash using a for loop



# Create an iterator for flash: superhero


# Print each item from the iterator
print(____)
print(____)
print(____)
print(____)
編輯並執行程式碼