Concepts / Reserved Keywords

Reserved Keywords

A reserved keyword is a word that Python keeps for its own syntax.

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

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

Classifying identifier names

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

  1. 1
    _count

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

  2. 2
    3_count

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

  3. 3
    count_3

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

  4. 4
    count-3

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

  5. 5
    count 3

    Invalid: a space is not allowed inside an identifier.

  6. 6
    __init__

    Valid: it starts with an underscore and uses only letters and underscores afterward.

Result

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.

countlowercase spellingCountcapitalized spellingCOUNTuppercase spellingdifferent identifierdifferent identifier
How does Python treat count, Count, and COUNT? Each capitalization pattern is a different identifier.
Worked example

Following capitalization precisely

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

  1. 1
    Compare the spellings

    myname uses a lowercase n, while myName uses an uppercase N.

  2. 2
    Apply case-sensitivity

    Python treats these spellings as different identifiers rather than as two ways to write the same name.

  3. 3
    Interpret the later reference

    The name myName has not been created merely because myname exists. Python reports that myName is not defined.

Result

Capitalization must match exactly whenever an identifier is used.

Reserved Words and Naming Choices

Definition

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

  • Common mistake

    Starting an identifier with a digit

    What it looks like
    3_count
    Why it fails

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

    Do this instead

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

  • Common mistake

    Using spaces between words

    What it looks like

    count 3

    Why it fails

    Spaces are not allowed inside an identifier.

    Do this instead

    Use underscores to separate words, such as count_3.

  • Common mistake

    Using a hyphen between words

    What it looks like
    count-3
    Why it fails

    Hyphens are not allowed identifier characters.

    Do this instead

    Replace the hyphen with an underscore, such as count_3.

  • Common mistake

    Changing capitalization accidentally

    What it looks like

    myname followed by myName

    Why it fails

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

    Do this instead

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

  • Common mistake

    Using a reserved keyword as a variable name

    What it looks like

    class, for, or if

    Why it fails

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

    Do this instead

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

Before

3_count count-3 count 3 myname and myName

The first three names contain invalid characters or positions; the last pair differs in capitalization.

After

count_3 count_3 count_3 myname and myname

The rewritten names use an allowed structure, and the repeated name uses matching capitalization.

Best practice

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

Related concepts

More concepts starting with R