100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

Keywords and Identifiers in Python

The difference between Python's reserved keywords and programmer-defined identifiers, plus the naming rules that make an identifier valid.

Basics & Data TypesBeginner7 min readJul 7, 2026
Analogies

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

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

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

python
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

text
True
False
True
if -> True
else -> True
while -> True
def -> True
return -> True
import -> True
10 20

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

Was this page helpful?

Topics covered

#Python#PythonProgrammingStudyNotes#Programming#KeywordsAndIdentifiersInPython#Keywords#Identifiers#Syntax#Explanation#StudyNotes#SkillVeris