Naming Conventions and Best Practices
A variable is a name that refers to a value, allowing you to store and manipulate data in your program.
A Name That Carries a Value
A variable is a name that refers to a value. This name-value relationship lets a program store and manipulate data. Assignment is the operation that establishes this relationship: a value is produced, and a variable name is bound to it.
Trace the Changing State
Consider three assignment statements: score = 5, score = 10, and bonus = 2. The important question is not only what each line says by itself. The important question is how the collection of name-value relationships changes after each line executes.
What do you think happens?
After score = 5, then score = 10, then bonus = 2, what value does score refer to, and what value does bonus refer to?
Reveal answer
Answer: score refers to 10 and bonus refers to 2
The first assignment creates score and binds it to 5. The second assignment uses the existing name score and updates its binding to 10. The third assignment creates bonus and binds it to 2.
Following Three Assignments
Determine the variables and values after score = 5, score = 10, and bonus = 2 execute in order.
First assignment: The right side produces the value 5. Because score is being used as a new variable name, Python creates score and binds it to 5.
Second assignment: The right side produces the value 10. The name score already exists, so Python updates that existing name-value relationship.
Third assignment: The right side produces the value 2. The name bonus is new, so Python creates bonus and binds it to 2.
The final relationships are score referring to 10 and bonus referring to 2.
How Assignment Works
The basic assignment syntax is variable_name = value. The equals sign in this statement means assign or bind; it does not mean equals in the sense of comparing two sides. Data flows from the right side toward the variable name on the left side.
- Python first evaluates the expression on the right side of the equals sign.
- That evaluation produces a value.
- Python creates a variable with the name on the left, or updates that name if it already exists.
- Python binds the variable name to the value produced by the right side.
Creating and Updating Names
Assignment can have two closely related effects. If the name on the left has not been used as a variable, the assignment creates a new variable and binds it to the evaluated value. If the name already exists, the assignment updates that existing variable's binding. Tracing the name after every line prevents the common mistake of treating every assignment as the creation of a completely separate variable.
Names That Explain Values
A variable name is the part of the relationship that a reader sees first. A useful naming practice is to select a name that helps identify the value it refers to. For example, total communicates more about its intended role than x in a generated assignment such as total = 25. The assignment mechanics are the same, but the first name gives a reader a clearer starting point for tracing the program.
| Name choice | Assignment | What the name communicates |
|---|---|---|
| Less informative | x = 25 | The name refers to a value, but the value's role is not identified. |
| More informative | total = 25 | The name gives a reader a useful clue about the value's role. |
Mistakes in Assignment Tracing
Reading the equals sign as a statement that two sides are permanently equal.
In an assignment statement, the equals sign means assign or bind. The later statement updates the existing name-value relationship.
Fix:
Read the right side as the value-producing side and the left side as the name being created or updated.Assuming Python assigns the left side before evaluating the right side.
Python evaluates the right side first, producing a value before binding the variable name.
Fix:
Trace the expression on the right first, then record the binding under the name on the left.Assuming every assignment creates a new variable.
The second statement uses a name that already exists, so it updates that existing variable.
Fix:
Check whether the left-side name is new or already present in the current program state.Choosing a name without considering the value it refers to.
The name-value relationship still works, but the shorter name gives less information to someone reading or tracing the program.
Fix:
Prefer a name that helps identify the referred value when the program is read.
Practice the State Trace
Trace these assignments in order: price = 8, price = 12, and tax = 3. For each line, write the current name-value relationships. Then explain which statement creates a new variable and which statement updates an existing one.
Hints
- Evaluate the right side of each assignment first.
- After price = 8, record the binding for price.
- When price = 12 executes, check whether price is already present.
- The name tax has not appeared in the earlier assignments.
Improve the name in this generated assignment without changing its value: x = 25. Explain how your replacement name would help a reader identify the value's role.
Hints
- The source material does not require a particular spelling convention.
- Choose a name that communicates more about the value than x does.
Key Takeaways
- A variable is a name that refers to a value.
- The assignment form is variable_name = value.
- The equals sign in assignment means assign or bind, and data flows from the right side to the left-side name.
- Python evaluates the right side first, then creates or updates the variable named on the left.
- Clearer variable names make name-value relationships easier to identify while tracing a program.
Key Takeaways
- A variable is a name that refers to a value.
- An assignment evaluates the right side first and binds the result to the name on the left.
- An assignment with a new name creates a variable; an assignment with an existing name updates it.
- Tracing each assignment in order reveals the program's current name-value relationships.
- Names that help identify their referred values make those relationships easier to follow.