TUTORIAL
Python Tutorial - Keywords and Identifiers
Introduction of Python 3 keywords and identifiers
On this page
Python uses keywords as part of its syntax and identifiers as names chosen by the programmer. Knowing the difference helps you read statements, check which words are reserved by your interpreter, and choose names that are both valid and clear.
Python Keywords
A keyword is a reserved token with a defined role in Python’s grammar. Words such as if, for, def, and class introduce or control language constructs; and, in, and is also have syntactic meaning. These spellings are case-sensitive: True, False, and None use capitals, while most other keywords are lowercase. A keyword cannot be used as an ordinary variable, function, or class name because the parser treats it as syntax.
The keyword set depends on the Python version. Avoid relying on a fixed count or an old copied list: inspect the interpreter that will run your code. The standard-library keyword module exposes that interpreter’s hard keywords through kwlist, and iskeyword() checks a candidate spelling. This also makes it easy to distinguish a reserved token from a name that merely looks important.
Run this example with your Python interpreter:
import keyword
candidate = "match"
print("Python keywords:", len(keyword.kwlist))
print("Is 'match' a keyword:", keyword.iskeyword(candidate))
total = 10
Total = 20
print("Names are case-sensitive:", total != Total)
Python keywords: 35
Is 'match' a keyword: False
Names are case-sensitive: True
The keyword count and the check for match depend on the Python version, so treat these results as specific to the interpreter used for this example and check the interpreter you intend to use. The example assigns values to total and Total separately, then compares those identifiers; Python treats the differently capitalized names as distinct bindings. In Python versions that support soft keywords, some words are reserved only in particular grammatical contexts; consult that interpreter’s documentation when a word’s status matters.
The operators in and is are keywords with different purposes. in tests membership, such as whether a value occurs in a collection, and also appears in a for loop. is tests object identity, not general value equality; use == when comparing values. See the Python operators reference for the distinction.
Python Identifiers
An identifier is a name used for a variable, function, class, module, or another named object. Python identifiers are case-sensitive, so total and Total are separate names. Under Python’s Unicode identifier rules, the first character must be an underscore or have the Unicode XID_Start property; each following character must have the XID_Continue property. These properties define the permitted characters more precisely than an informal list of letters and digits. Identifiers cannot contain spaces or punctuation such as $ or !, and a hard keyword cannot be used as an identifier. The rules are broader than ASCII: names such as café can be valid, although teams may choose a narrower character set for portability and ease of typing.
Python has no practical fixed identifier-length rule for ordinary code, but extremely long names are difficult to read. Use descriptive names and separate words consistently. customer_count follows the common snake_case style for variables and functions; capitalization patterns such as CustomerRecord are conventions often used for classes. These are style choices, not syntax requirements.
You can ask a string whether it has identifier syntax with str.isidentifier(). That check answers only whether the spelling is valid as an identifier; it does not reject keywords by itself. Use keyword.iskeyword() as a separate check when accepting a name dynamically. An identifier also does not determine what an object is or whether two names refer to the same object: those are separate questions from naming syntax.
The following complete example checks candidate names and demonstrates that changing capitalization creates a distinct identifier.
import keyword
names = ["customer_count", "2customers", "customer-count", "class", "Customer"]
for name in names:
valid = name.isidentifier() and not keyword.iskeyword(name)
print(f"{name}: {'valid' if valid else 'not a usable identifier'}")
customer = "Northwind"
Customer = "Contoso"
print(customer, Customer)
customer_count: valid
2customers: not a usable identifier
customer-count: not a usable identifier
class: not a usable identifier
Customer: valid
Northwind Contoso