Python共有35个关键字:and、as、assert、break、class、continue、def、del、elif、else、except、False、finally、for、from、global、if、import、in、is、lambda、None、nonlocal、not、or、pass、raise、return、True、try、while、with、yield,大小写敏感,可通过import keyword; print(keyword.kwlist)查看。

Python关键字是语言中预定义的保留词,具有特殊语法功能,不能用作变量名或标识符。目前Python 3.10+版本共有35个关键字。
Python有哪些关键字?
以下是Python的全部关键字列表:
and as assert break class continue def del elif else except False finally for from global if import in is lambda None nonlocal not or pass raise return True try while with yield注意:这些词大小写敏感,比如 True、False、None 首字母大写,而其他如 if、for 是小写。
怎么查看当前Python版本的关键字?
最简单的方法是使用Python内置的 keyword 模块。
立即学习“Python免费学习笔记(深入)”;
在交互式环境中输入:
import keywordprint(keyword.kwlist)
这会输出当前Python版本中所有关键字的列表。
你也可以检查某个单词是否是关键字:
for = 10 # 报错:invalid syntax小技巧:记住常用关键字
不需要背下全部35个,日常使用频率高的大概十几种:
- if / elif / else:条件控制
- for / while:循环
- def / return:定义函数
- class:定义类
- import / from:导入模块
- try / except / finally:异常处理
- True / False / None:基本常量
- and / or / not:逻辑运算
基本上就这些。用得多了自然就熟悉了。关键是别拿它们当变量名就行。











