- 零基础Python学习笔记
- 明日科技编著
- 333字
- 2024-01-24 15:57:30
2.1.1 保留字
保留字是Python已经被赋予特定意义的一些单词,在开发程序时,不可以把这些保留字作为变量、函数、类、模块和其他对象的名称来使用。Python的保留字如表2.1所示。
表2.1 Python中的保留字

学习笔记
Python中的所有保留字是区分字母大小写的。例如,True、if是保留字,但是TRUE、IF就不属于保留字,如图2.1和图2.2所示。

图2.1 True是保留字,但true不属于保留字

图2.2 if是保留字,但IF不属于保留字
学习笔记
Python中的保留字可以通过在IDLE中,输入以下两行代码查看:

运行结果如图2.3所示。

图2.3 查看Python中的保留字
学习笔记
如果在开发程序时,使用Python中的保留字作为模块、类、函数或变量等的名称,则会提示“invalid syntax”的错误信息。在下面代码中使用了Python保留字if作为变量的名称:

程序运行时会出现如图2.4所示的错误提示信息。

图2.4 使用Python保留字作为变量名时的错误信息