Python reserved words, also known as Python keywords, are predefined terms with special meaning in the language. These reserved keywords in Python form the foundation of Python’s syntax, program structure, and control flow.
Understanding how Python keywords work is essential for writing clean, readable, and error-free Python code, especially when learning Python basic syntax.
1. What Are Python Reserved Keywords?
Python Reserved keywords in Python are predefined words that define program logic, execution flow, and structure. You cannot use Python keywords as variable names, function names, or identifiers because they already serve a specific purpose for the Python interpreter.
Think of Python keywords like road signs in programming — they guide the interpreter on how to execute your code correctly and consistently. Common Python keywords include:if, else, while, def, class, try, except, import, and more. 2. Number of Python Reserved Words
As of Python 3.10 and later, there are 35 reserved keywords. You can dynamically view the current list of Python keywords using the built-in keyword module.
import keyword
print(keyword.kwlist)
print("Total keywords:", len(keyword.kwlist))
Output Example:
['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']
Total keywords: 35
Using the keyword module is the most reliable way to verify Python reserved words across versions. 3. Full List of Python Reserved Words
False await else import pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield
4. Example of Syntax Error Using Python Keywords
Python does not allow reserved keywords to be used as identifiers.
def = 5 #SyntaxError: invalid syntax
Correct usage:
definition = 5
Explanation:
Since def is a Python keyword used to define functions, using it as a variable name results in a syntax error.
5. Categories of Python Keywords (With Examples)
Python reserved words (keywords) can be grouped into 9 logical categories. Understanding these categories helps you write structured, readable, and Pythonic code.i) Boolean & Null Constants in Python Keywords
These keywords represent truth values or the absence of a value.| Keyword | Meaning | Example |
|---|---|---|
| True | Boolean true value | is_active = True |
| False | Boolean false value | is_logged_in = False |
| None | Represents “nothing” | value = None |
is_logged_in = True
if is_logged_in:
print("Welcome back!")
Explanation:
True and False are Boolean constants, while None represents a null or empty value.
ii) Control Flow Keywords
Control the execution path of a Python program.
| Keyword | Purpose |
|---|---|
| if, else, elif | Conditional branching |
| for, while | Loops |
| break, continue | Loop control |
| pass | Placeholder statement |
age = 20
if age >= 18:
print("You can vote")
else:
print("Too young")
Explanation:
Control flow keywords decide which code runs and when, forming the backbone of program logic.
iii) Keywords for Functions and Classes
Used to define reusable logic and object-oriented structures.
def greet(name):
return f"Hello, {name}"
square = lambda x: x * x
print(square(5))
iv) Exception Handling Keywords
Used for handling runtime errors safely.
try:
num = int("abc")
except ValueError:
print("Invalid number!")
finally:
print("Done trying.")
v) Modules and Imports
Used to include external or built-in modules.
import math
print(math.sqrt(16))
from datetime import datetime as dt
print(dt.now())
vi) Variable Scope & Global Context
Control variable visibility and lifetime.
x = 5
def change():
global x
x = 10
vii) Logical & Membership Keywords
Used for logical operations and comparisons.
| Keyword | Description |
|---|---|
| and | Logical AND |
| or | Logical OR |
| not | Logical NOT |
| is | Identity comparison |
| in | Membership test |
viii) Async Programming Keywords
import asyncio
async def greet():
await asyncio.sleep(1)
print("Hello Async")
ix) Assertions & Context Managers
Used for debugging and resource handling.
assert 2 + 2 == 4
with open("file.txt", "r") as f:
data = f.read()
6. Can You Use Keywords as Variable Names?
No. Python keywords cannot be used as variable names.
class = "Beginner" #
Correct alternatives:
class_name = "Beginner"
7. Real-Life Analogy
Think of Python keywords as predefined commands in a video game. You can use them to perform actions, but you cannot redefine what “jump” or “move” means. Similarly, Python keywords control how programs behave.
8. Summary Table of Python Keywords
| Category | Keywords |
|---|---|
| Constants | True, False, None |
| Control Flow | if, else, elif, for, while, break, continue, pass |
| Functions & Classes | def, return, class, lambda |
| Error Handling | try, except, finally, raise |
| Imports | import, from, as |
| Scope & Deletion | global, nonlocal, del |
| Logical Operators | and, or, not, is, in |
| Async Programming | async, await |
| Assertions & Context | assert, with |
9. Key Takeaways
- Python has 35 reserved keywords that control syntax and structure.
- Keywords cannot be used as identifiers.
- Understanding Python keywords helps you write clean, readable, and maintainable code.