Python Keywords: Reserved Words You Must Know
Introduction
In this chapter, you will learn Python keywords, which are reserved words with special meaning in the language. Keywords are essential because they define Python syntax and control program behavior. If you use them correctly, your code becomes clear, valid, and professional.
Prerequisites
- Python
3.10+installed - Basic understanding of variables, operators, and loops
- Ability to run
.pyfiles in terminal or IDE
What Is a Keyword
A keyword is a reserved word that Python already uses for language rules.
Examples:
if,else,for,whileTrue,False,Nonedef,class,return
You cannot use keywords as variable names.
# This is valid
user_name = "Alice"
# This is invalid because 'class' is a keyword
# class = "Python"1) Common Keyword Groups
You do not need to memorize all keywords on day one. Start with practical groups.
Flow Control Keywords
if,elif,elsefor,whilebreak,continue,pass
# Decide access based on age
age = 20
if age >= 18:
print("Access granted")
else:
print("Access denied")Function and Class Keywords
def,returnclasslambda
# Define a simple function
def add(a, b):
return a + b
print(add(2, 3))Boolean and Null-like Keywords
True,FalseNone
# Boolean values
is_online = True
# None means no value yet
middle_name = None
print(is_online, middle_name)Error Handling Keywords
try,except,finallyraise
try:
# Convert input to integer
value = int(input("Enter a number: "))
print(value)
except ValueError:
# Handle invalid input
print("Please enter a valid integer.")Import and Scope Keywords
import,from,asglobal,nonlocal
# Import with alias
import math as m
# Use imported function
print(m.sqrt(16))2) How to Check Python Keywords
Python provides a built-in way to list all current keywords.
# Import keyword module
import keyword
# Print all keywords
print(keyword.kwlist)This is useful because keyword sets can evolve between versions.
Tip
Best Practice
When unsure whether a word is reserved, check with keyword.kwlist instead of guessing.
3) Why Keywords Matter in Real Code
Keywords are not just theory. They are part of every script:
ifandforfor logic and loopsdefandreturnfor reusable functionstryandexceptfor robust error handling
If you misunderstand keywords, code may fail to run or become hard to maintain.
4) Common Beginner Mistakes
Mistake 1: Using a Keyword as a Variable Name
# Invalid variable name
# for = 10Fix by renaming:
# Valid variable name
for_count = 10
print(for_count)Mistake 2: Confusing == and is
== compares values; is checks object identity.
For beginners, use == for normal value comparison.
Mistake 3: Ignoring Indentation Around Keyword Blocks
Keywords like if, for, and def require correctly indented blocks.
Bad indentation causes IndentationError or wrong logic.
Warning
Do not force-memorize every keyword at once.
Focus on the ones you use daily (if, for, def, return, try, except), then expand naturally.
Mini Practice
Write a script that:
- asks user for a score
- uses
if / elif / elseto print grade level - uses
try / exceptto handle invalid input
Reference implementation:
try:
# Read score from user
score = float(input("Enter your score: "))
# Classify score
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: D")
except ValueError:
# Handle non-numeric score
print("Invalid input. Please enter a number.")FAQ
Do keywords change between Python versions?
Sometimes, yes. Most core keywords stay stable, but newer versions may add or adjust some.
Is print a keyword?
No. print is a built-in function, not a keyword.
Should I memorize all keywords now?
No. Learn them by usage. Daily coding will make them stick naturally.
How do I avoid keyword naming conflicts?
Use descriptive names like class_name, for_count, or is_active instead of reserved words.