Keywords มีความหมายพิเศษใน Syntax ไม่สามารถใช้เป็น Identifier (ชื่อตัวแปร ฟังก์ชัน คลาส) ได้
รายการ Keywords Python 3 (33 คำ)
| and | except | lambda | with |
| as | finally | nonlocal | while |
| assert | false | None | yield |
| break | for | not | |
| class | from | or | |
| continue | global | pass | |
| def | if | raise | |
| del | import | return | |
| elif | in | True | |
| else | is | try |
Python 2 vs 3
- Python 2: 30 Keywords (มี
print,exec) - Python 3: 33 Keywords (
print→ Function, เพิ่มnonlocal,False/True/Noneเป็น Keyword)
ดูรายการใน Interpreter
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', ...]
เคล็ดลับ: IDE 部分会 Highlight Keywords ให้อัตโนมัติ ช่วยป้องกันการใช้ผิด