Keywords in Python are predefined, reserved words that possess special architectural meaning to the CPython compiler and interpreter. Because they define the syntax structure of the language, keywords cannot be used as variable names, function names, class names, or any other identifier. In this tutorial, you will explore all 35 reserved keywords categorized by purpose, along with modern soft keywords introduced in Python 3.10+.
Python provides a built-in module named keyword that allows you to inspect all currently reserved keywords directly in your shell:
import keyword
print("Total Python Keywords:", len(keyword.kwlist))
print(keyword.kwlist)
# Check if a word is a keyword:
print("Is 'def' a keyword?", keyword.iskeyword('def')) # True
print("Is 'total' a keyword?", keyword.iskeyword('total')) # False
| Category | Keywords | Description & Functional Purpose |
|---|---|---|
| Boolean & Null Values | True, False, None |
Truth values and Python's singleton null value. Notice these 3 are capitalized! |
| Logical Operators | and, or, not |
Evaluate Boolean logic conditions without symbols (&&, ||). |
| Control Flow & Branching | if, elif, else |
Evaluate conditional branching and decision logic paths. |
| Looping & Iteration | for, while, break, continue, pass |
Iterate over sequences, jump out of loops, skip iterations, or create empty placeholders. |
| Functions & Generators | def, return, lambda, yield |
Define functions, return values, create anonymous expressions, and pause generator iterators. |
| Object-Oriented Programming | class |
Define user-created classes and custom object blueprints. |
| Modules & Imports | import, from, as |
Import external libraries and assign alias names. |
| Exception Handling | try, except, finally, raise, assert |
Safely catch runtime exceptions, guarantee cleanup, and validate test conditions. |
| Variable Scope | global, nonlocal |
Modify variables residing in module global or enclosing function scopes. |
| Context Managers & Async | with, async, await |
Manage resources automatically (files/locks) and write asynchronous concurrent coroutines. |
| Deletion & Membership | del, in, is |
Delete object references, check sequence membership, and verify object identity. |
Starting in Python 3.10, Python introduced soft keywords: match, case, and _ (used in Structural Pattern Matching). Unlike hard keywords, soft keywords are only reserved when used inside pattern matching syntax, meaning you can still use a variable named match = 10 in older legacy code without breaking it!
# Modern Python Structural Pattern Matching (match-case)
http_status = 404
match http_status:
case 200:
print("OK: Success!")
case 404:
print("Error: Page Not Found!")
case 500:
print("Server: Internal Server Error!")
case _:
print("Unknown HTTP Status Code")
Attempting to assign a value to a reserved keyword triggers a syntax compilation error before the code even executes:
# Invalid Python Code:
pass = "Secret123"
# SyntaxError: invalid syntax
def = 50
# SyntaxError: invalid syntax
True, False, and None must start with a capital letter. Writing true or false will raise a NameError: name 'true' is not defined.
x = None binds the variable x to the singleton None object. In contrast, del x deletes the variable name x completely from the local namespace dictionary, so referencing x afterward raises a NameError.