Python Keywords Explained (Beginner Guide)
Python keywords are reserved words that have a special meaning in the language.
They matter because Python uses them to understand the structure of your code. If you use a keyword in the wrong place, you will usually get a syntax error. As a beginner, learning to recognize keywords will make Python code much easier to read.
You do not need to memorize every keyword right away. Start by understanding what keywords are, what they do, and how to avoid using them as names.
Quick way to check Python keywords
Python includes a built-in module named keyword that can show you the current keyword list and check whether a word is reserved.
import keyword
print(keyword.kwlist)
print(keyword.iskeyword("if"))
print(keyword.iskeyword("name"))
What this does:
keyword.kwlistshows all Python keywordskeyword.iskeyword("if")returnsTruekeyword.iskeyword("name")returnsFalse
This is useful when you are not sure whether a word is allowed as a variable name.
What Python keywords are
Keywords are reserved words with special meaning in Python.
That means:
- You cannot use them as variable names
- You cannot use them as function names
- You cannot use them as class names
Examples of Python keywords include:
ifelseforwhiledefclassreturnimport
For example, this code is invalid because for is a keyword:
for = 10
Python will raise an error because for already has a special meaning in the language.
Why keywords matter
Keywords are important because Python uses them to understand how your program is written.
For example:
ifstarts a conditionforstarts a loopdefstarts a functionreturnsends a value back from a function
If Python sees one of these words, it treats it as part of the language syntax, not as a normal name.
Using a keyword incorrectly often causes a syntax error, such as SyntaxError: invalid syntax.
Recognizing keywords also helps you read code faster. When you see if, for, or def, you can immediately tell what kind of code block you are looking at.
Common Python keywords beginners should know first
You do not need the full keyword list on day one. These are the most useful ones to learn first.
Condition keywords
ifelifelse
These are used to make decisions in code. If you are new to conditions, see Python if statements explained.
Loop keywords
forwhilebreakcontinuepass
These are used for repetition and loop control. To go deeper, read Python for loops explained.
Function keywords
defreturnlambda
These are related to functions. If you want the basics first, see Python functions explained.
Import keyword
import
This is used to bring code in from modules. For more on that, see how import works in Python.
Boolean and logic keywords
TrueFalseNoneandornot
These are used in conditions and logical expressions.
Exception handling keywords
tryexceptfinallyraise
These are used when handling errors in your program.
How keywords are different from built-in functions
This is a very common beginner confusion.
Keywords
Keywords are part of Python syntax.
Examples:
iffordefreturn
You do not call a keyword like a function. For example, this is valid syntax:
if 5 > 3:
print("Yes")
Here, if is a keyword. It does not use parentheses as a function call.
Built-in functions
Built-in functions are names you can call.
Examples:
print()len()type()
These use parentheses because you are calling them as functions:
print("Hello")
print(len("Python"))
So:
ifis a keywordprint()is a functionlen()is a function
If you want to learn more about these, see Python print() function explained and Python len() function explained.
Example: keywords used in real code
Here is a short program that uses several common keywords:
def get_message(number):
if number > 0:
return "Positive"
else:
return "Zero or negative"
for value in [3, 0, -2]:
print(get_message(value))
Output:
Positive
Zero or negative
Zero or negative
Keywords used here
def— starts the function definitionif— checks a conditionreturn— sends a value back from the functionelse— runs when theifcondition is falsefor— loops through the listin— used as part of theforloop syntax
What the code does
- It defines a function called
get_message - The function checks whether a number is greater than zero
- It returns a string based on the result
- A
forloop then calls the function for each number in a list
This is a good example of how keywords help form the structure of Python code.
If you want help understanding the bigger picture, Python syntax basics explained is a good next step.
How to see the full keyword list in Python
You can use the keyword module from Python’s standard library.
import keyword
print(keyword.kwlist)
This prints the full list of keywords for the version of Python you are using.
You can also check one word at a time:
import keyword
print(keyword.iskeyword("for"))
print(keyword.iskeyword("my_variable"))
Possible output:
True
False
This is useful because keyword lists can change slightly between Python versions.
If you want to run these checks quickly from the command line, you can use:
python -c "import keyword; print(keyword.kwlist)"
python -c "import keyword; print(keyword.iskeyword('for'))"
python -c "import keyword; print(keyword.iskeyword('my_variable'))"
Common beginner mistakes with keywords
Here are some of the most common problems beginners run into.
Trying to use a keyword as a variable name
This will fail:
class = "Beginner"
Use a different name instead:
class_name = "Beginner"
Confusing keywords with built-in functions
Many beginners think print and len are keywords.
They are not. They are built-in functions.
This matters because:
- functions are called with parentheses
- keywords are part of syntax
Forgetting that True, False, and None are special names
These are not ordinary variable names. They have special meaning in Python.
For example:
value = True
empty = None
These names are part of Python itself and should be used as intended.
Misspelling a keyword
Small spelling mistakes can break your code.
Example:
x = 10
elf x > 5:
print("Big")
This is invalid because elf is not the keyword elif.
Misspelled keywords may cause:
- a syntax error
- a name error
- confusing behavior if you do not notice the typo
Copying code without understanding reserved words
It is easy to copy code that uses words like try, return, or pass without knowing what they do.
A better approach is to pause and ask:
- Is this word a keyword?
- What role does it play in the code?
- Is it starting a block, returning a value, or controlling flow?
What to learn next
Once you can recognize Python keywords, the next step is learning how they work together in real syntax.
Good next topics are:
- Python syntax basics explained
- Python if statements explained
- Python for loops explained
- Python functions explained
- How import works in Python
The goal is not just to spot keywords, but to use them correctly in simple Python programs.
FAQ
What is a keyword in Python?
A keyword is a reserved word that Python uses as part of its syntax, such as if, for, and def.
Can I use a Python keyword as a variable name?
No. Keywords are reserved and will cause an error if you try to use them as names.
Is print a keyword in Python?
No. print() is a built-in function, not a keyword.
How do I see all Python keywords?
Use the keyword module and run print(keyword.kwlist).
Do Python keywords change?
Sometimes. The list can change slightly between Python versions.