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四川省專科大學名單!附2024年錄取分數線參考 - 天天要聞

2025四川省專科大學名單!附2024年錄取分數線參考

四川專科學校包括四川交通職業技術學院、成都航空職業技術學院、四川鐵道職業學院等。2024年四川專科公辦學校分數較低的有成都工業職業技術學院(409分)、綿陽職業技術學院(411分)、四川航天職業技術學院(412分)等。
大漲!杭二630分,學軍629分,杭高627分,杭州市區第一批高中錄取分數線出爐 - 天天要聞

大漲!杭二630分,學軍629分,杭高627分,杭州市區第一批高中錄取分數線出爐

點擊上圖查看詳情今天下午,2025年杭州市區各類高中集中統一第一段招生錄取完成,各校錄取分數線出爐。2025年杭州市區各類高中集中統一招生第一段學校錄取分數線(陸續更新中)橙柿記者在現場了解到,今年部分分數段的考生集中,比如620分以上,每個分數段都有一批人,1分可能有100-200人。所以,同分滑檔的情況較明顯。杭...