Concepts / Understanding Variables and Assignment

Understanding Variables and Assignment

Variable names must start with a letter or underscore, contain only letters, digits, and underscores, and can be any length.

  • Programming

Why Names Matter

A variable name is more than a label added to a value. When you create a variable, you give a value a name that will appear throughout your code. That name becomes a contract with anyone reading the code, including your future self: it should communicate what the value represents. Python therefore places hard limits on which names are legal, while naming conventions help you make legal names readable.

A useful variable name has to satisfy two tests: Python must accept its character pattern, and a reader should be able to understand what the variable represents.

The First-Character Test

Python variable names must start with a letter or an underscore. A digit cannot be the first character. This rule applies before Python checks the rest of the name, so changing only the first character can change a name from legal to illegal. A name that starts with a letter or underscore can then contain letters, digits, and underscores.

letterlegal first character_legal first character7illegal first character-illegal first character
How does changing the first character between a letter, underscore, digit, or symbol change whether a variable name is legal?

Checking the Start of a Name

Decide whether each generated name passes the first-character rule.

customer: It begins with a letter, so its first character follows the rule.

_customer: It begins with an underscore, which is also allowed.

2customer: It begins with a digit, so Python does not accept it as a legal variable name.

-customer: It begins with a symbol, so it does not follow the variable-name rule.

The first two names pass the first-character test; the last two do not.

The Complete Character Pattern

The first-character rule is only part of the check. A complete Python variable name may contain letters, digits, and underscores, but it may not contain spaces, hyphens, or other special characters. A digit is therefore allowed after the first character, while a space or hyphen makes the name illegal. Names can be any length, so length itself does not make a variable name invalid.

totallettersitem countspaceitem2digit after first characteritem-counthyphenitem_countunderscoreitem$countspecial character
Which character patterns make a complete variable name valid or invalid in Python?
PatternResultReason
LettersAllowedLetters may appear in a variable name.
Digits after the first characterAllowedDigits may be included after the name has started with a letter or underscore.
UnderscoresAllowedUnderscores are among the characters Python permits in variable names.
SpacesNot allowedSpaces cause a syntax error.
HyphensNot allowedHyphens cause a syntax error.
Other special charactersNot allowedSpecial characters cause a syntax error.

The character categories that determine whether a complete variable name follows the stated Python rules.

Assignment as Naming

Assignment connects a variable name with a value. The important idea is that creating a variable is not merely about keeping a value available; it is about giving that value a meaningful name that can appear throughout the code. The name should tell readers what the value represents.

namesitem_countvariable name42assigned value
How does assignment connect a variable name to the value it stores or refers to?

Compare a vague generated name such as x with a descriptive generated name such as item_count. Both may be legal character patterns, but item_count communicates what the value represents while x does not. A legal name is therefore only the starting point; the name should also serve its contract with the reader.

Readable Naming Conventions

Python convention uses snake_case for regular variables. Snake_case uses lowercase words separated by underscores, which improves readability and consistency. For example, the generated name total_price communicates more than totalprice because the word boundary is visible. The convention does not change which characters Python permits; it gives people a consistent way to combine permitted characters into meaningful names.

Name styleReadabilityGuidance
total_priceWords are separated clearlyFollows the snake_case convention for a regular variable.
totalpriceWords are less visually distinctIt may be legal, but it is less readable than the snake_case form.
xPurpose is unclearA legal name is not necessarily self-documenting.

Common Naming Mistakes

  • Starting a variable name with a digit

    Variable names must start with a letter or underscore. A digit cannot be the first character.

    Fix: Use a name that begins with a letter or underscore, such as item7 or items.

  • Using spaces in a name

    Spaces are not permitted in variable names and cause a syntax error.

    Fix: Use an underscore to separate words, such as item_count.

  • Using a hyphen to separate words

    Hyphens are not permitted in variable names and cause a syntax error.

    Fix: Use snake_case, such as item_count.

  • Using a special character

    Special characters are not among the permitted letters, digits, and underscores.

    Fix: Remove the special character and choose a name made from permitted characters.

  • Assuming every underscore use improves a regular application name

    Leading underscores are valid, but the source guidance reserves them for library code.

    Fix: For a regular application variable, prefer item_count unless the library-code convention is intentional.

  • Choosing a legal but vague name

    The name may satisfy Python's character rules but does not communicate what the value represents.

    Fix: Choose a self-documenting snake_case name related to the value's purpose.

Naming Practice

MEDIUM

For each generated name, decide whether it follows Python's character rules. If it is legal, decide whether it is also readable and convention-following: customer_name, 3customers, customer-name, customer_name2, _customer_name, and customercount.

Hints
  • Check the first character first.
  • Then look for spaces, hyphens, or other special characters.
  • For names that are legal, consider whether lowercase words separated by underscores would communicate the purpose more clearly.
  • Remember that a leading underscore is valid but is reserved for library code according to the source guidance.

A Three-Part Name Check

Evaluate the generated name customer_name2.

First character: The name starts with a letter, so it passes the first-character rule.

Remaining characters: It contains letters, an underscore, and a digit after the first character. These are permitted character categories.

Readability: The lowercase words are separated with an underscore, matching the snake_case convention. The name is therefore both legal and relatively descriptive.

customer_name2 follows the stated character rules and the regular-variable naming convention.

Key Takeaways

  1. A Python variable name must start with a letter or underscore.
  2. After the first character, a name may contain letters, digits, and underscores; spaces, hyphens, and special characters are not allowed.
  3. Variable names can be any length, but readable names communicate what their values represent.
  4. Snake_case uses lowercase words separated by underscores and is the convention for regular variables.
  5. Leading underscores are valid but should be avoided in regular applications because the source guidance reserves them for library code.
  6. Assignment gives a value a name, so a variable name should act as a clear contract with the reader.

Key Takeaways

  • Start every Python variable name with a letter or underscore.
  • Use only letters, digits, and underscores in the complete name; digits cannot come first.
  • Use lowercase snake_case names to make regular variables readable and consistent.
  • Treat underscores carefully: internal underscores improve word separation, while leading underscores are intended for library-code conventions.
  • Remember that assignment gives a value a name, making the name's meaning part of the code's readability.