Understanding Variables and Assignment
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.
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.
Checking the Start of a Name
Decide whether each generated name passes the first-character rule.
- 1customer
It begins with a letter, so its first character follows the rule.
- 2_customer
It begins with an underscore, which is also allowed.
- 32customer
It begins with a digit, so Python does not accept it as a legal variable name.
- 4-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.
| Pattern | Result | Reason |
|---|---|---|
| Letters | Allowed | Letters may appear in a variable name. |
| Digits after the first character | Allowed | Digits may be included after the name has started with a letter or underscore. |
| Underscores | Allowed | Underscores are among the characters Python permits in variable names. |
| Spaces | Not allowed | Spaces cause a syntax error. |
| Hyphens | Not allowed | Hyphens cause a syntax error. |
| Other special characters | Not allowed | Special 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.
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 style | Readability | Guidance |
|---|---|---|
| total_price | Words are separated clearly | Follows the snake_case convention for a regular variable. |
| totalprice | Words are less visually distinct | It may be legal, but it is less readable than the snake_case form. |
| x | Purpose is unclear | A legal name is not necessarily self-documenting. |
Generated examples showing how naming style affects communication, not just legality.
Common Naming Mistakes
Common mistake Starting a variable name with a digit
What it looks like7items
Why it failsVariable names must start with a letter or underscore. A digit cannot be the first character.
Do this insteadUse a name that begins with a letter or underscore, such as item7 or items.
Common mistake Using spaces in a name
What it looks likeitem count
Why it failsSpaces are not permitted in variable names and cause a syntax error.
Do this insteadUse an underscore to separate words, such as item_count.
Common mistake Using a hyphen to separate words
What it looks likeitem-count
Why it failsHyphens are not permitted in variable names and cause a syntax error.
Do this insteadUse snake_case, such as item_count.Common mistake Using a special character
What it looks likeitem$count
Why it failsSpecial characters are not among the permitted letters, digits, and underscores.
Do this insteadRemove the special character and choose a name made from permitted characters.
Common mistake Assuming every underscore use improves a regular application name
What it looks like_item_countWhy it failsLeading underscores are valid, but the source guidance reserves them for library code.
Do this insteadFor a regular application variable, prefer item_count unless the library-code convention is intentional.
Common mistake Choosing a legal but vague name
What it looks likex
Why it failsThe name may satisfy Python's character rules but does not communicate what the value represents.
Do this insteadChoose a self-documenting snake_case name related to the value's purpose.
Naming Practice
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.
- 1First character
The name starts with a letter, so it passes the first-character rule.
- 2Remaining characters
It contains letters, an underscore, and a digit after the first character. These are permitted character categories.
- 3Readability
The lowercase words are separated with an underscore, matching the
snake_caseconvention. The name is therefore both legal and relatively descriptive.
customer_name2 follows the stated character rules and the regular-variable naming convention.
Key Takeaways
- 1A Python variable name must start with a letter or underscore.
- 2After the first character, a name may contain letters, digits, and underscores; spaces, hyphens, and special characters are not allowed.
- 3Variable names can be any length, but readable names communicate what their values represent.
- 4Snake_case uses lowercase words separated by underscores and is the convention for regular variables.
- 5Leading underscores are valid but should be avoided in regular applications because the source guidance reserves them for library code.
- 6Assignment gives a value a name, so a variable name should act as a clear contract with the reader.
At a glance
- 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_casenames 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.