1. Introduction
Every Python program is built from two kinds of names: keywords, which are reserved words with special meaning built into the language, and identifiers, which are the names programmers choose for variables, functions, classes, and modules. Understanding the boundary between the two is essential to avoid syntax errors and to write clear, self-documenting code.
Cricket analogy: Keywords are like the fixed Laws of Cricket (LBW, no-ball) that no team can redefine, while identifiers are like the player names a team chooses freely for its own squad sheet.
Keywords like if, for, def, and class cannot be repurposed as variable names because the interpreter relies on them to parse the structure of your program, while identifiers follow a small set of naming rules that keep code unambiguous and readable.
Cricket analogy: You can't rename the LBW rule to mean something else mid-match just as if, for, def can't be repurposed -- but a team can freely call its new batsman anything, following simple naming rules.
2. Syntax
import keyword
keyword.iskeyword("for") # True -> reserved word
keyword.iskeyword("data") # False -> valid identifier
_valid_name = 10 # starts with underscore
validName2 = 20 # letters + digit, not starting with digit3. Explanation
Python reserves around 35 keywords (the exact count varies slightly by version) such as if, elif, else, for, while, def, class, return, import, True, False, and None. These words are hard-coded into Python's grammar and cannot be used as variable, function, or class names — attempting to do so raises a SyntaxError.
Cricket analogy: Python's ~35 reserved keywords are like cricket's fixed dismissal types (LBW, run-out, stumped) -- a fixed, hard-coded list you cannot invent a new one from, or the umpire (interpreter) raises an error.
An identifier, by contrast, is any name you choose for a variable, function, class, or module, subject to a few rules: it must start with a letter (a-z, A-Z) or an underscore, followed by any combination of letters, digits, and underscores; it cannot contain spaces or symbols like -; and it is case-sensitive, so Total and total are different identifiers.
Cricket analogy: An identifier is like a player's chosen nickname -- it must start with a letter (not a number like '7Sharma'), can't contain spaces or dashes, and 'Kohli' and 'kohli' count as two different names.
Use keyword.iskeyword("word") from the built-in keyword module to programmatically check whether a given string is a reserved keyword before using it as a name.
Common gotcha: newer Python versions also have 'soft keywords' like match, case, and _ (used in structural pattern matching) that act specially only in certain contexts but can still be used as ordinary identifiers elsewhere — unlike true keywords, which can never be used as identifiers at all.
4. Example
import keyword
print(keyword.iskeyword("for"))
print(keyword.iskeyword("data"))
print(keyword.iskeyword("class"))
sample_keywords = ["if", "else", "while", "def", "return", "import"]
for word in sample_keywords:
print(word, "->", keyword.iskeyword(word))
_valid_name = 10
valid_name2 = 20
print(_valid_name, valid_name2)5. Output
True
False
True
if -> True
else -> True
while -> True
def -> True
return -> True
import -> True
10 206. Key Takeaways
- Keywords are reserved words (if, for, def, class, ...) built into Python's grammar and cannot be used as identifiers.
- Identifiers are programmer-chosen names for variables, functions, classes, and modules.
- Identifiers must start with a letter or underscore and contain only letters, digits, and underscores.
- Identifiers are case-sensitive, so total and Total are different names.
- The keyword module's iskeyword() function checks whether a string is a reserved keyword.
- Soft keywords like match/case behave specially only in context and can still be used as regular identifiers.
Practice what you learned
1. Which of the following is a Python keyword and therefore cannot be used as a variable name?
2. What does keyword.iskeyword("data") return?
3. Which identifier is valid according to Python's naming rules?
4. Are Python identifiers case-sensitive?
5. What happens if you try to use a reserved keyword like `return` as a variable name?
6. What distinguishes a 'soft keyword' like `match` from a true keyword like `if`?
Was this page helpful?
You May Also Like
Variables in Python
Learn how Python variables work as dynamically-typed name bindings, how to create and reassign them, and the rules for naming them correctly.
Comments in Python
How to write single-line and multi-line comments in Python, and the difference between comments and docstrings.
Functions in Python
How to define, call, and document reusable blocks of code with Python functions, including the def keyword and implicit None return.
Operators in Python
An overview of Python's arithmetic, comparison, logical, assignment, bitwise, identity, and membership operators with precedence rules.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics