Reserved Keywords
A reserved keyword is a word that Python keeps for its own syntax.
Why Names Need Rules
When you write score = 42, score is an identifier: a name given to a variable or another Python entity. The name labels the value 42 so that you can retrieve it, modify it, or use it in calculations. Python needs strict rules for names so the interpreter can reliably parse your code and distinguish one name from another.
A reserved keyword is a word that Python keeps for its own syntax. Names such as class, for, and if are not ordinary names available for variables because Python uses them for language constructs. An identifier must also obey character and capitalization rules. Therefore, choosing a variable name involves two checks: the spelling must have a valid identifier form, and the name must not be reserved by Python.
Checking an Identifier
Python's identifier rules can be checked from left to right. The first character must be a letter or an underscore. After that first character, the name may contain letters, underscores, or digits. Spaces and hyphens are not allowed. A digit may therefore appear later in a name, but not at the beginning.
Classifying identifier names
Decide whether _count, 3_count, count_3, count-3, count 3, and __init__ follow Python's identifier rules.
- 1_count
Valid: an underscore is allowed as the first character, and the remaining characters are allowed.
- 23_count
Invalid: the name starts with a digit. Digits can appear after the first character, not before it.
- 3count_3
Valid: the name starts with a letter, and the digit appears later.
- 4count-3
Invalid: a hyphen is not one of the allowed identifier characters.
- 5count 3
Invalid: a space is not allowed inside an identifier.
- 6__init__
Valid: it starts with an underscore and uses only letters and underscores afterward.
The valid names are _count, count_3, and __init__. The invalid names are 3_count, count-3, and count 3.
Reading Names Exactly
Python identifiers are case-sensitive. Uppercase and lowercase letters are treated as different characters, so myname and myName are completely different identifiers. The same distinction applies to names such as count, Count, and COUNT. They may look related to a human reader, but Python treats each spelling as a separate name.
Following capitalization precisely
A name is created with the spelling myname. What happens when the spelling myName is used later?
- 1Compare the spellings
myname uses a lowercase n, while
myNameuses an uppercase N. - 2Apply case-sensitivity
Python treats these spellings as different identifiers rather than as two ways to write the same name.
- 3Interpret the later reference
The name
myNamehas not been created merely because myname exists. Python reports thatmyNameis not defined.
Capitalization must match exactly whenever an identifier is used.
Reserved Words and Naming Choices
Reserved keywords are words Python reserves for its syntax instead of allowing them to be used as ordinary variable names. The source examples include class, for, and if. These words must retain their language meaning, so they cannot be chosen as variable identifiers.
This restriction is separate from the character rules. A name can have the right shape and still be unavailable because Python has reserved it. Conversely, a name that is not reserved can still be invalid if it begins with a digit or contains a space or hyphen.
| Name choice | Identifier character rules | Reserved-word issue | Result |
|---|---|---|---|
| score | Follows the character rules | Not identified as reserved in the source examples | Valid form |
| class | Has an identifier-like character form | Reserved for Python syntax | Cannot be used as an ordinary variable name |
| 3_count | Starts with a digit | Reserved-word issue is not the problem | Invalid form |
| count-3 | Contains a hyphen | Reserved-word issue is not the problem | Invalid form |
A name must satisfy both the character rules and the restriction on reserved keywords.
Mistakes Beginners Make
Common mistake Starting an identifier with a digit
What it looks like3_countWhy it failsThe first character must be a letter or underscore. Python uses this restriction to distinguish identifiers from numbers.
Do this insteadRewrite the name so that it begins with a letter or underscore, such as count_3.
Common mistake Using spaces between words
What it looks likecount 3
Why it failsSpaces are not allowed inside an identifier.
Do this insteadUse underscores to separate words, such as count_3.
Common mistake Using a hyphen between words
What it looks likecount-3Why it failsHyphens are not allowed identifier characters.
Do this insteadReplace the hyphen with an underscore, such as count_3.
Common mistake Changing capitalization accidentally
What it looks likemyname followed by myName
Why it failsPython is case-sensitive, so these are different identifiers.
Do this insteadUse exactly the same capitalization each time the identifier is referenced.
Common mistake Using a reserved keyword as a variable name
What it looks likeclass, for, or if
Why it failsThese words are reserved for Python syntax rather than ordinary variable names.
Do this insteadChoose a different identifier that follows the character rules and is not reserved.
3_count count-3 count 3 myname and myName
The first three names contain invalid characters or positions; the last pair differs in capitalization.
count_3 count_3 count_3 myname and myname
The rewritten names use an allowed structure, and the repeated name uses matching capitalization.
Use snake_case for variable names: lowercase words separated by underscores, such as player_score. This convention is not enforced by Python, but it is the style experienced Python programmers expect to see. It helps readers distinguish separate words without using spaces or hyphens.
Name-Checking Practice
Classify each name as valid or invalid, and give the rule that decides your answer: player_score, PlayerScore, 4players, _players, players-3, and COUNT.
Hints
- Check the first character before checking the rest.
- Look for spaces or hyphens.
- Compare capitalization exactly.
- Remember that an underscore may be the first character and a digit may appear later.
- 1To check a Python identifier, first verify that its first character is a letter or underscore. Then verify that every later character is a letter, underscore, or digit. Reject names containing spaces or hyphens, reject reserved keywords such as class, for, and if, and compare capitalization exactly because Python is case-sensitive.
Key Takeaways
- An identifier is a name given to a variable or another Python entity.
- A valid identifier starts with a letter or underscore and then uses only letters, underscores, or digits.
- Spaces, hyphens, and leading digits make identifier names invalid.
- Python is case-sensitive, so count, Count, and COUNT are different identifiers.
- Reserved keywords such as class, for, and if cannot be used as ordinary variable names.