There are some specific keywords that cannot be used as identifiers in the python language.
These keywords are called “reserved keywords” or “keywords of the language”.
Here is the list of 36 reserved keywords in python language.
False | assert | def | for | is | raise |
None | async | del | from | lambda | return |
True | await | elif | global | nonlocal | try |
__peg_parser__ | break | else | if | not | while |
and | class | except | import | or | with |
as | continue | finally | in | pass | yield |
These are not ordinary keywords or you cannot assign any value to these words.
Note: There are three keywords in the above list that start with the capital letter i.e.; False, True, None.
If you will try to assign the value to any of the above-mentioned keywords, the syntax error will appear. For example:
Code:
and = 2300
print(and)
Output:
and = 2300
^
SyntaxError: invalid syntax
How to print Python reserved words list?
You can display the list of reserved keywords using the “keywords.kwlist” in python.
Code:
import keyword
print(keyword.kwlist)
Output:
['False', 'None', 'True', '__peg_parser__', 'and', 'as', 'assert', 'async', 'await', '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']