Concepts / Reserved Keywords

Reserved Keywords

Identifiers are names given to variables and other entities; they must follow strict syntax rules.

  • Programming

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.

same accepted groupsame accepted groupsame rejected groupsame rejected groupscoreletter first3_countdigit firstcount_3digit after startcount-3hyphen included_privateunderscore firstcount 3space included
Which identifier names are accepted by Python, and which are rejected because they start with a digit, contain invalid characters, or use spaces?

Classifying identifier names

Decide whether _count, 3_count, count_3, count-3, count 3, and __init__ follow Python's identifier rules.

_count: Valid: an underscore is allowed as the first character, and the remaining characters are allowed.

3_count: Invalid: the name starts with a digit. Digits can appear after the first character, not before it.

count_3: Valid: the name starts with a letter, and the digit appears later.

count-3: Invalid: a hyphen is not one of the allowed identifier characters.

count 3: Invalid: a space is not allowed inside an identifier.

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

different identifierdifferent identifiercountlowercase spellingCountcapitalized spellingCOUNTuppercase spelling
How does Python treat count, Count, and COUNT? Each capitalization pattern is a different identifier.

Following capitalization precisely

A name is created with the spelling myname. What happens when the spelling myName is used later?

Compare the spellings: myname uses a lowercase n, while myName uses an uppercase N.

Apply case-sensitivity: Python treats these spellings as different identifiers rather than as two ways to write the same name.

Interpret the later reference: The name myName has not been created merely because myname exists. Python reports that myName is 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 choiceIdentifier character rulesReserved-word issueResult
scoreFollows the character rulesNot identified as reserved in the source examplesValid form
classHas an identifier-like character formReserved for Python syntaxCannot be used as an ordinary variable name
3_countStarts with a digitReserved-word issue is not the problemInvalid form
count-3Contains a hyphenReserved-word issue is not the problemInvalid form

A name must satisfy both the character rules and the restriction on reserved keywords.

Mistakes Beginners Make

  • Starting an identifier with a digit

    The first character must be a letter or underscore. Python uses this restriction to distinguish identifiers from numbers.

    Fix: Rewrite the name so that it begins with a letter or underscore, such as count_3.

  • Using spaces between words

    Spaces are not allowed inside an identifier.

    Fix: Use underscores to separate words, such as count_3.

  • Using a hyphen between words

    Hyphens are not allowed identifier characters.

    Fix: Replace the hyphen with an underscore, such as count_3.

  • Changing capitalization accidentally

    Python is case-sensitive, so these are different identifiers.

    Fix: Use exactly the same capitalization each time the identifier is referenced.

  • Using a reserved keyword as a variable name

    These words are reserved for Python syntax rather than ordinary variable names.

    Fix: Choose a different identifier that follows the character rules and is not reserved.

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

MEDIUM

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.
  1. To 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.