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 添加冒號。

教育分類資訊推薦

卑微的,不一定是塵埃(二) - 天天要聞

卑微的,不一定是塵埃(二)

一九八零年十月份,我正式入學洪山技校(不久改為淄博礦務局技校,後更名山東煤炭技術學院)。這座建於五十年代的技校,是煤炭部批准成立最早的一批技校,師資力量雄厚。後來成立的淮北礦院或山東礦院,都曾從這個技校抽調教師。入學技校最大的好處是家裡不用
2025貴州中小學期末考試、放假及開學時間 - 天天要聞

2025貴州中小學期末考試、放假及開學時間

元旦假期剛剛結束緊隨而來的就是學生老師的寒假啦那麼貴州省中小學何時期末考?本學期什麼時候結束?寒假什麼時候放?一起來看看吧! 期末考試時間安排 1-6年級:2025年1月14日-....
15年一女教師選擇辭職看世界,如今過去了10年,她怎樣了 - 天天要聞

15年一女教師選擇辭職看世界,如今過去了10年,她怎樣了

「讀萬卷書不如行萬里路」,這句古語道出了親身經歷的重要性。然而,真正付諸行動的人卻寥寥無幾。顧少強的辭職信2015年,一位女教師留下十字辭職信,毅然放棄「鐵飯碗」,只為環遊世界。這一決定在網上掀起軒然大波,無數人開始關注她的後續發展。令人意
平川區長征街道「紅領巾」寒假託管班開班 - 天天要聞

平川區長征街道「紅領巾」寒假託管班開班

1月13日,平川區長征街道「紅領巾」寒假託管班開班。開班首日,由專業心理老師通過互動遊戲、案例分享,引導孩子們正確認識情緒,提升心理調適能力。現場氣氛熱烈,孩子們積极參与,在歡樂的氛圍中收穫了寶貴的心理健康知識。
【實戰大練兵】清水縣公安局組織開展公文寫作考試 - 天天要聞

【實戰大練兵】清水縣公安局組織開展公文寫作考試

「筆」拼促提升,妙手見真「章」。為檢驗和提升廣大民輔警公文寫作能力,發現和培養更多優秀寫作人才,更好地服務保障公安中心工作。1月13日,清水縣公安局組織開展公文寫作考試。副縣長、縣公安局黨委書記、局長張建卓作動員講話,並圍繞現代警務建設出題