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 .py files 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, while
  • True, False, None
  • def, class, return

You cannot use keywords as variable names.

python
# 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, else
  • for, while
  • break, continue, pass
python
# Decide access based on age
age = 20
 
if age >= 18:
    print("Access granted")
else:
    print("Access denied")

Function and Class Keywords

  • def, return
  • class
  • lambda
python
# Define a simple function
def add(a, b):
    return a + b
 
print(add(2, 3))

Boolean and Null-like Keywords

  • True, False
  • None
python
# Boolean values
is_online = True
 
# None means no value yet
middle_name = None
 
print(is_online, middle_name)

Error Handling Keywords

  • try, except, finally
  • raise
python
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, as
  • global, nonlocal
python
# 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.

python
# 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:

  • if and for for logic and loops
  • def and return for reusable functions
  • try and except for 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

python
# Invalid variable name
# for = 10

Fix by renaming:

python
# 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:

  1. asks user for a score
  2. uses if / elif / else to print grade level
  3. uses try / except to handle invalid input

Reference implementation:

python
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.