保留字即关键字,我们不能把它们用作任何标识符名称。Python的标准库提供了一个keyword模块,可以输出当前版本的所有关键字
代码:
import keyword print(keyword.kwlist)运行结果
[‘False’, ‘None’, ‘True’, ‘and’, ‘as’, ‘assert’, ‘break’, ‘class’, ‘continue’, ‘def’, ‘del’, ‘elif’, ‘else’, ‘except’, ‘finally’, ‘for’, ‘from’, ‘global’, ‘if’, ‘import’, ‘in’, ‘is’, ‘lambda’, ‘nonlocal’, ‘not’, ‘or’, ‘pass’, ‘raise’, ‘return’, ‘try’, ‘while’, ‘with’, ‘yield’]
本文介绍了Python中的关键字概念及其作用,并提供了获取Python所有关键字的具体方法。通过使用Python的标准库keyword模块,我们可以轻松地输出当前版本的所有关键字。
1152

被折叠的 条评论
为什么被折叠?



