Python Keywords
Level: 1 | Version: v1.0 | Author: Meptrasoft
What is a Keyword?
A keyword is a reserved word that has a fixed, special meaning in Python.
Because keywords are reserved by Python for building code structure, you cannot use keywords as variable names, function names, or class names.
# Incorrect: Using 'if' (a keyword) as a variable name raises a SyntaxError
if = 10 # Wrong
# Correct: Use a normal name
if_value = 10 # Correct
Output:
KEYWORD → RESERVED WORD IN PYTHON → CANNOT BE USED AS A VARIABLE NAME
How Many Keywords Are There in Python?
In current Python versions, there are 35 reserved keywords.
You can check the full list of keywords directly in Python using the built-in keyword module:
import keyword
# Print all keywords in your Python version
print(keyword.kwlist)
# Print total count of keywords
print(len(keyword.kwlist)) # Output: 35
You can also check whether any word is a keyword using keyword.iskeyword():
import keyword
print(keyword.iskeyword("for")) # True (for is a keyword)
print(keyword.iskeyword("total")) # False (total is a normal variable name)
All 35 Keywords Grouped by Category
The diagram below shows the main keyword groups. The table after it gives a complete beginner-friendly list of all 35 reserved keywords.
Here is a complete breakdown of the 35 reserved keywords by category:
| Category | Keywords | What They Do |
|---|---|---|
| Values | True, False, None |
Represent Boolean truth values and the absence of a value |
| Conditions | if, elif, else |
Used for making decisions in code |
| Loops | for, while, break, continue, pass |
Used to repeat actions or control loop flow |
| Functions & Classes | def, return, lambda, yield, class |
Used to create functions and classes |
| Logic & Comparison | and, or, not, in, is |
Used for logical tests and membership checks |
| Imports & Scope | import, from, as, global, nonlocal |
Used to load modules and manage variable scope |
| Error Handling | try, except, finally, raise, assert |
Used to handle runtime errors gracefully |
| Other Control Keywords | with, del, async, await |
Used for context managers, deleting names, and asynchronous programming |
Note: Only three keywords start with a capital letter in Python:
True,False, andNone. All other keywords are written in lowercase.
Placement Quick Points
- Python has 35 reserved keywords.
- Keywords cannot be used as variable names.
- Attempting to use a keyword as a variable name raises a
SyntaxError. - Only
True,False, andNonestart with a capital letter. - Use
keyword.kwlistto see the full list of keywords. - Use
keyword.iskeyword("word")to check if a word is a keyword.
Interview Questions
- What is a keyword in Python?
- A reserved word that has a fixed, predefined meaning in Python syntax.
- Can you use
classas a variable name in Python? - No.
classis a reserved keyword; using it as a variable name raises aSyntaxError. - Which Python keywords start with a capital letter?
- Only three:
True,False, andNone. - How can you check if a word is a Python keyword using code?
- Import the
keywordmodule and usekeyword.iskeyword("word"). - What error occurs if you use a keyword as a variable name?
SyntaxError: invalid syntax.
Practice
- Write a program using
import keywordto print all 35 Python keywords. - Check if
eval,exec,async, andawaitare Python keywords usingkeyword.iskeyword(). - Fix the syntax error in this code:
for = "Fourth Year Student"
- Name three keywords used for conditional statements.
- Name three keywords used for handling exceptions/errors.
End of Python Keywords