Python基礎: 列表遍歷

2024年12月06日07:23:14 教育 1745


Python基礎: 列表遍歷 - 天天要聞

遍歷整個列表:

For 循環:

  • 使用列表項自動執行重複性任務。
  • 示例: for magician in magicians: print(magician) .
magicians = ['Alice', 'David', 'carolina']
 for magician in magicians:
    print(magician)

>>

alice
 david
 carolina

迭代所有項:

  • Loop 檢索並處理每個項目。
  • 示例:在列表中列印每個魔術師的名字。
magicians = ['alice', 'david', 'carolina']
 for magician in magicians:
    print(f"{magician.title()}, that was a great trick!")

>>

 Alice, that was a great trick!
 David, that was a great trick!
 Carolina, that was a great trick!

循環執行:

  • 對每個列表項重複上述步驟。
  • 示例:使用循環的個性化消息。
magicians = ['alice', 'david', 'carolina']
 for magician in magicians:
    print(f"{magician.title()}, that was a great trick!")
    print(f"I can't wait to see your next trick, {magician.title()}.\n")

>>

Alice, that was a great trick!
I can't wait to see your next trick, Alice.

David, that was a great trick!
I can't wait to see your next trick, David.

Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.

循環後操作:

  • 循環後的代碼執行一次。
  • 示例:在單獨消息後感謝所有魔術師。
magicians = ['alice', 'david', 'carolina']
 for magician in magicians:
    print(f"{magician.title()}, that was a great trick!")
    print(f"I can't wait to see your next trick, {magician.title()}.\n")
 print("Thank you, everyone. That was a great magic show!")


>>

Alice, that was a great trick!
 I can't wait to see your next trick, Alice.

 David, that was a great trick!
I can't wait to see your next trick, David.

 Carolina, that was a great trick!
 I can't wait to see your next trick, Carolina.

 Thank you, everyone. That was a great magic show!

避免 Python 中的縮進錯誤

忘記縮進:

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)  # Should be indented

#Error : 
File "magicians.py", line 3
    print(magician)
    ^
 IndentationError: expected an indented block after 'for' statement on line 2
  • 錯誤: IndentationError: expected an indented block after 'for' statement
  • 修復:縮進 print(magician) 行。

忘記縮進額外的行:

for magician in magicians:
    print(f"{magician.title()}, that was a great trick!")
print(f"I can't wait to see your next trick, {magician.title()}.\n")  # Should be indented


#OutPut

Alice, that was a great trick!
 David, that was a great trick!
 Carolina, that was a great trick!
 I can't wait to see your next trick, Carolina.
  • 問題:只有最後一個魔術師會收到第二條消息,因為該行不在循環內。
  • 修復:縮進第二個 print 語句。

不必要地縮進:

message = "Hello Python world!"
    print(message)  # Unnecessary indent

#Error : 

File "hello_world.py", line 2
    print(message)
    ^
 IndentationError: unexpected indent
  • 錯誤: IndentationError: unexpected indent
  • 修復:刪除不必要的縮進。

在循環後縮進:

for magician in magicians:
    print(f"{magician.title()}, that was a great trick!")
    print(f"I can't wait to see your next trick, {magician.title()}.\n")
    print("Thank you everyone, that was a great magic show!")  # Should not be indented


>>

Alice, that was a great trick!
I can't wait to see your next trick, Alice.
Thank you everyone, that was a great magic show!

 David, that was a great trick!
 I can't wait to see your next trick, David.
 Thank you everyone, that was a great magic show!

 Carolina, that was a great trick!
 I can't wait to see your next trick, Carolina.
 Thank you everyone, that was a great magic show
  • 問題:感謝信息是為每位魔術師列印的。
  • 修復:取消縮進最後一個 print 語句。

忘記冒號:

for magician in magicians  # Missing colon
    print(magician)


#Error : 

 File "magicians.py", line 2
    for magician in magicians
                             ^
 SyntaxError: expected ':'
  • 錯誤:語法錯誤:應為「:」
  • 修復:magicians 中為 magician 添加冒號。

教育分類資訊推薦

學生放棄清北,老師解散群聊,誰該反思? - 天天要聞

學生放棄清北,老師解散群聊,誰該反思?

不報清華北大就解散群聊?最近,江西一位老師的做法引發爭議。據江西瑞昌一中最新通報,該校某班班主任漆老師建議高分學生填報「清北」相關專業志願,結果有3名學生放棄清北、另擇他校,導致老師在微信群內發表了不當言論並解散群聊。網傳群聊圖片顯示,該老師認為學生放棄清北,是「將我們所有的感情都化整為零」,是「班...
教育部最新通知!事關教師發展 - 天天要聞

教育部最新通知!事關教師發展

近日,教育部辦公廳印發《關於組織實施數字化賦能教師發展行動的通知》,一起來看具體內容——來源/ 四川教育發布編輯/ 毛莉泓校對/ 馬儷伲責編/ 楊雪審核/ 肖宵監製/ 譚榮皓往期回顧夏日去哪兒玩?甘孜州兩個景區獲國家級推介近期熱門視頻分享、收藏、點贊、再看安排一下...
多所高校強基計劃錄取分數線公布! - 天天要聞

多所高校強基計劃錄取分數線公布!

目前北京大學、清華大學等多所高校公布2025年強基計劃錄取分數線及錄取結果️️️北京大學北京大學2025年強基計劃測試和錄取結果已公布,考生可登錄北京大學2025年強基計劃信息平台查詢。✅查詢入口https://www.ccuut.edu.
美國塔科馬市體育場中學代表團將再訪福州高級中學 - 天天要聞

美國塔科馬市體育場中學代表團將再訪福州高級中學

美國塔科馬市體育場中學代表團將再訪福州高級中學 中美青少年以歌會友延續友誼 福州高級中學合唱團在排練《舟中曉望》。 2025年「鼓嶺緣」中美青少年合唱周啟幕在即,美國華盛頓州塔科馬市的體育場中學代表團即將開啟他們的福州之旅。自2019年與福州高級中學簽署交流合作備忘錄以來,該校將再次造訪福高,延續這份跨越太平...
2025河南定向培養軍士政治考核和體格檢查分數線公布! - 天天要聞

2025河南定向培養軍士政治考核和體格檢查分數線公布!

大象新聞·河南交通廣播記者 宋冰霜7月8日,記者從河南省教育考試院了解到,河南省2025年普通高校招生定向培養軍士政治考核和體格檢查控制分數線「出爐」,具體信息如下:根據河南省教育廳、公安廳和省軍區動員局《2025年招收定向培養軍士工作實施
南京復讀哪個高中最好 - 天天要聞

南京復讀哪個高中最好

選擇復讀高中是一個重要的決定,需要綜合考慮多方面的因素。南京作為江蘇省的省會城市,擁有眾多的高中學校,其中不乏一些優秀的高中。然而,要判斷哪個高中最適合復讀,需要根據個人情況和需求來做出決策。以下是一些可以幫助您選擇南京復讀高中的建議:1.
廣州在園幼兒降8%,小學招生少近12% - 天天要聞

廣州在園幼兒降8%,小學招生少近12%

製圖:易福紅(豆包AI) 南都訊 記者楊曉彤 近日,廣州市教育局發布《2024年廣州市教育事業發展統計公報》。根據2024年廣州市教育事業統計數據結果,全市共有各級各類學校3895所,在校生302.03萬人,專任教師19.24萬人。 學前教育 學前教育毛入學率127.77% 2024年,全市共有幼兒園2224所。比上年減少22所,下降0.98%。其中...