類別 | 保留字 | 說明 |
---|---|---|
控制流程 | if , else , elif |
條件判斷 |
for , while , break , continue , pass |
迴圈控制 | |
try , except , finally , raise , assert |
例外處理 | |
函式與類別 | def , return , lambda |
函式定義與返回 |
class , yield |
類別與產生器 | |
邏輯與運算 | and , or , not , in , is |
邏輯與運算子 |
變數與模組 | import , from , as , global , nonlocal |
模組與變數作用域 |
其他 | del , with , async , await , None , True , False |
其他特殊關鍵字 |
import keyword
print(keyword.kwlist) # 取得 Python 保留字列表
is
比較的是 物件的記憶體位址,==
比較的是 值是否相等。global
用於修改全域變數,nonlocal
用於修改巢狀函式內的變數。_
來區分def_
、return_value
# 語法錯誤
def = 10
import = "data.csv"
# 正確命名
class_ = "Python"