Naming Variables: Rules and Best Practices
Keywords are reserved words used by Python to define program structure and cannot be used as variable names.
A Name Has a Role
When you name a variable, you are choosing a user-defined identifier: a name for a value in your program. Python also has its own reserved words. These keywords are used to define program structure, so they are not available for variable naming. The central question is therefore not only whether a name looks readable, but also whether Python has already assigned that word a special structural role.
How Python Interprets Names
Python must interpret each name according to the role it plays in a statement. A user-defined variable name is available for the programmer to choose, while a reserved keyword already belongs to Python's program-structure vocabulary. If the same word is placed where a variable name is expected, Python cannot treat it as an ordinary user-defined name. The statement is therefore syntactically invalid and produces a SyntaxError.
Reserved keyword: a word reserved by Python to define program structure. It cannot be used as a variable name.
The Reserved Set
Python has 35 specific reserved keywords that are off-limits for variable names. The important recognition skill is to separate this fixed reserved set from names that a programmer is allowed to define. A word may look short, familiar, or readable and still be unavailable if Python reserves it for program structure.
| Name category | Python's treatment | Can it be a variable name? |
|---|---|---|
| User-defined identifier | Chosen by the programmer | Yes, when it follows the naming rules |
| Reserved keyword | Used to define program structure | No |
The key distinction is whether Python has already reserved the word for program structure.
Shape Rules for Names
A variable name can fail before keyword checking if its shape violates Python's naming rules. Variable names cannot start with a number. They also cannot contain most special characters, including characters such as @ or $. These restrictions are separate from the reserved-keyword rule: a name can be invalid because of its characters, or because the entire word is reserved for Python's program structure.
Classifying Three Candidate Names
Decide why each generated candidate should not be used as a Python variable name.
Candidate A: A candidate that begins with a number violates the rule that variable names cannot start with a number.
Candidate B: A candidate containing @ violates the restriction on most special characters.
Candidate C: A candidate that is one of Python's reserved keywords cannot be used as a variable name because the word already defines program structure.
The three candidates fail for two different kinds of reasons: invalid name shape and reserved-word conflict.
Mistakes That Cause SyntaxError
Using a reserved keyword as a variable name
Python already uses that word for program structure, so it cannot also be interpreted as an ordinary user-defined variable name.
Fix:
Choose a different user-defined identifier that is not in the reserved set.Starting a variable name with a number
Variable names cannot start with a number.
Fix:
Begin the name with an allowed non-numeric character instead.Putting @ or $ inside a variable name
Variable names cannot contain most special characters, including these examples.
Fix:
Remove the restricted special character and choose a name that follows the naming rules.
When Python reports a SyntaxError near a proposed variable name, inspect the name in two passes. First, check its shape: does it begin with a number, or does it contain a character such as @ or $? Second, check its role: is the complete word one of Python's 35 reserved keywords? This process distinguishes a malformed name from a name that conflicts with Python's program structure.
A Naming Decision Routine
- Start with the complete candidate name rather than judging only part of it.
- Check that the name does not start with a number.
- Check that it does not contain restricted special characters such as @ or $.
- Check whether the complete word is one of Python's 35 reserved keywords.
- If any check fails, select another user-defined identifier before continuing.
For each of the following generated descriptions, identify the rule that must be checked first: a name beginning with a digit, a name containing $, a word used by Python for program structure, and a name that passes those three checks. Explain whether the issue is name shape, reserved-word status, or no issue identified by the rules in this article.
Hints
- A leading digit is a shape problem.
- The dollar sign is a special-character problem.
- A word used for program structure must be checked against the reserved set.
- Passing these checks does not require inventing additional restrictions not covered here.
What to Remember
- User-defined identifiers are names chosen for variables, while reserved keywords are reserved by Python for program structure.
- Python has 35 reserved keywords, and none of them can be used as variable names.
- Using a reserved keyword where Python expects a variable name creates a syntax conflict and causes a SyntaxError.
- Variable names cannot start with a number or contain most special characters, including @ and $.
- When diagnosing an invalid name, check both its character pattern and its reserved-word status.
Key Takeaways
- A variable name must be available for programmer definition and must not be a reserved Python keyword.
- The reserved keyword set contains 35 words used to define program structure.
- A reserved keyword used as a variable name creates a SyntaxError because Python cannot assign it both roles.
- Names beginning with numbers or containing characters such as @ or $ violate variable-naming rules.