Module 3 · Variables and Data Types — topic 3 of 15
Naming rules
The rules and guidelines for variable names in one place: legal characters, reserved words, case sensitivity, avoiding built-in names, and choosing names that make code read clearly.
Learning objectives
- Apply the legal rules for variable names
- Avoid shadowing built-in names
- Choose clear, consistent variable names
Definition
The rules and guidelines for variable names in one place: legal characters, reserved words, case sensitivity, avoiding built-in names, and choosing names that make code read clearly.
Introduction
Module 2 introduced identifiers and naming conventions. This lesson brings them together specifically for variables and adds the most common naming trap: accidentally reusing the name of something Python already provides.
Why it matters
Naming is one of the hardest and most important parts of programming. Good names make code obvious; poor ones make it misleading. A shadowed built-in name can also cause errors far away from where the mistake was made.
Detailed explanation
Legal rules: letters, digits and underscores; not starting with a digit; not a keyword; case-sensitive.
Style rules (PEP 8): lower-case words joined by underscores, such as exam_date; UPPER_CASE for values meant to stay constant.
Shadowing: Python has built-in functions and types such as list, dict, str, sum, max, min, input and id. Assigning to one of those names replaces it for the rest of the file. Later code that expects the built-in then fails with confusing errors.
Meaningful names describe what a value represents, not its type: prices beats price_list, and average_mark beats float1.
Short names are fine when their scope is tiny and their meaning obvious, such as i in a three-line loop. Longer-lived variables deserve fuller names.
Avoid names that look alike: l (lower-case L), O (capital o) and I (capital i) are easily confused with 1 and 0.
Syntax
- Descriptive snake_case for ordinary variables.
- UPPER_CASE for constants.
- A one-letter name only for a short loop counter.
Code examples
Clear names explain themselves
9500
Checking what a name refers to
builtin_function_or_method 6
sum is a built-in function, so it shouldn’t be used as a variable name.
More examples
What shadowing breaks
total: 225 Error: 'int' object is not callable built-in again: 225
Step-by-step explanation
- sum = 0 makes the name sum refer to an integer, hiding the built-in function.
- The loop works, but now sum is the number 225.
- Calling sum(marks) tries to call an integer, which raises TypeError.
- del sum removes our variable, so the name falls back to the built-in function.
Expected output
Running “What shadowing breaks” prints exactly:
Practical applications
- Linters such as Ruff warn when a variable shadows a built-in (rule A001).
- Code reviews frequently rename vague variables such as data, temp and value.
- Shared projects agree on vocabulary so the same concept has the same name everywhere.
Best practices
- Never use built-in names such as list, dict, str, sum, id or input for variables.
- Name by meaning, not by type.
- Keep the same name for the same concept throughout a program.
Common mistakes
Naming a list list
Avoid
Prefer
Later calls to list() in the same file will fail because list now refers to your data.
Names that lie
Avoid
Prefer
A name should describe what the value really is.
Key takeaways
- Follow the legal identifier rules and PEP 8 style.
- Don’t shadow built-in names.
- Descriptive names make comments unnecessary.
Try it yourself
- 1Rename these to good variable names: x (a student’s age), lst (a list of prices), s (a sentence).
- 2Write code that shadows max, observe the error when you call max() afterwards, then fix it.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhich statement about Python names is true?