4.6 Keywords
What Python keywords are, the complete keyword list for Python 3.12, using help('keywords'), and why keywords can't be used as identifiers.
What Are Keywords?
What Is It?
Reserved words that are part of Python’s own syntax (if, for, def, class, …). The language grammar gives them special meaning, so they cannot be used as ordinary names.
Why Does It Matter?
Attempting to use a keyword as a variable name is a SyntaxError, not a warning.
>>> class = 5
File "<stdin>", line 1
class = 5
^^^^^
SyntaxError: invalid syntax
Complete Keyword List
Python 3.12 has 35 keywords, retrievable programmatically via the keyword module:
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', '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']
>>> len(keyword.kwlist)
35
Python’s 35 keywords, grouped by purpose.
Using help('keywords')
How Is It Used?
The interactive help system lists and explains every keyword directly from the interpreter, without needing internet access:
>>> help("keywords")
Here is a list of the Python keywords. Enter any keyword to get more help.
False class from or
None continue global pass
...
Restrictions
Keywords cannot be used as variable, function, or class names. Use keyword.iskeyword() to check programmatically before using a name you’re unsure about:
>>> keyword.iskeyword("for")
True
>>> keyword.iskeyword("variable")
False
Quick Interview Answer
“Keywords are the 35 reserved words baked into Python’s grammar —
if,for,def,class,import, and so on. They can never be used as an identifier; trying raises aSyntaxErrorimmediately, not a runtime warning. You can list them at any time withkeyword.kwlistor check a specific name withkeyword.iskeyword().”
Common Mistakes
- Trying to name a variable
class,type, orlist—classis a hard keyword (fails outright);typeandlistare built-in names, not keywords, so they’re technically legal but silently shadow the built-in. - Assuming the keyword count never changes across versions — soft keywords like
match,case, and_(added in 3.10) behave contextually and aren’t part ofkeyword.kwlist.
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form