When programming in Python, you can generally name variables, functions, and classes freely. However, there is one exception: words that are “reserved” for Python’s syntax itself, known as Reserved Words (Keywords). These words are used to define the structure of the program (such as “if this,” “repeat while,” etc.), so they cannot be used as identifiers like variable names.
This article explains what Python’s reserved words are, why they cannot be used, and lists common examples.
What Happens When You Use a Reserved Word as a Variable Name
If you try to use a reserved word as a variable name, the Python interpreter recognizes it as a grammatical error (SyntaxError), and the program will not run.
Examples of Errors:
# Trying to use the reserved word 'if' as a variable name
# if = 100
# SyntaxError: invalid syntax
# Trying to use 'def' as a variable name instead of a function definition
# def = "function_name"
# SyntaxError: invalid syntax
# Trying to use 'for' as a variable name
# for_count = 5 # This is OK (It is not 'for' itself)
# for = 5 # SyntaxError: invalid syntax
As shown with for_count, it is fine if the reserved word is just a part of the name. You are only prohibited from using the reserved word exactly as it is.
Classification and Examples of Major Reserved Words
Python’s reserved words can be classified by their function. Here are some typical examples.
1. Control Flow
Used to control the order of execution.
if,elif,else(Conditional branching)for,while(Loops)break,continue(Interrupt or skip loops)pass(Do nothing)
2. Functions and Classes (Structure Definition)
Used to define functions or classes (blueprints).
def(Define function)return(Return a value)class(Define class)lambda(Anonymous function)
3. Modules
Used to load other files (modules).
import(Load module)from(Load specific features from a module)as(Give an alias)
4. Logic and Values
Special values representing truth or emptiness. These cannot be variable names.
TrueFalseNone(No value exists)and,or,not(Logical operators)
5. Exception Handling
Defines what to do when an error occurs.
try,except,finallyraise(Intentionally cause an error)
Difference Between Reserved Words and “Built-in Functions”
In Python, there are words that look like reserved words but are different: “Built-in Functions” and “Built-in Types”.
Examples: print(), len(), str(), list(), sum()
Since these are not reserved words, using them as variable names does not cause a SyntaxError. However, if you use these names as variables, you overwrite the standard functionality provided by Python. This causes unexpected errors (like TypeError) later when you try to use the original function.
# 'list' is not a reserved word (it is a built-in type)
list = [10, 20, 30] # This does not cause a SyntaxError, but is NOT recommended
print(list) # Displays the content of variable 'list': [10, 20, 30]
# If you try to use the original list() function (type conversion), it fails
# numbers = list("123")
# TypeError: 'list' object is not callable
As shown above, using names like list or str for variables is technically possible but leads to serious bugs and should be strictly avoided. If names conflict, it is common to use my_list or list_.
Summary
- Reserved Words (Keywords) are words that make up Python’s grammar (e.g.,
if,for,def). - You cannot use reserved words as variable, function, or class names; doing so causes a
SyntaxError. - Built-in functions like
listorstrare not reserved words, but using them as variable names overwrites their original functionality, so you should avoid it.
