Guide Python Beginner

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.

2 min read

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
flowchart TD K["35 Python Keywords"] K --> CF["Control Flow\nif / elif / else\nfor / while\nbreak / continue / pass"] K --> FC["Functions & Classes\ndef / return / class\nlambda / yield"] K --> LV["Logical / Values\nand / or / not\nTrue / False / None\nin / is"] K --> EH["Error Handling & Scope\ntry / except / finally / raise\nglobal / nonlocal\nimport / from / as"]

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 a SyntaxError immediately, not a runtime warning. You can list them at any time with keyword.kwlist or check a specific name with keyword.iskeyword().”

Common Mistakes

  • Trying to name a variable class, type, or listclass is a hard keyword (fails outright); type and list are 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 of keyword.kwlist.

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form