Naming Conventions and Best Practices
A useful naming practice is to select a name that helps identify the value it refers to.
Try it: Assignment, Statement by Statement
How an assignment statement evaluates its right side first (using the names' current values) and only then binds the name on the left: updating x = x + 1, augmented operators like += and //=, chained a = b = 0, simultaneous a, b = b, a + b versus one-at-a-time assignment, rebinding a name to a value of another type, the NameError for a name that was never assigned, and the SyntaxError for a name that breaks the naming rules.
How it works
- Before anything runs, Python compiles the whole program; a name that breaks the rules (starts with a digit, contains a space or a dash, or is a keyword) is a SyntaxError and no line runs at all.
- For each assignment, evaluate the complete right side first, looking every name up in the variable table as it is now.
- Then bind the name(s) on the left to that value: a new name is added to the table, an existing one is replaced (even by a value of a different type).
- x += 5 reads x, applies the operator (+, -, *, //, %, **) and binds the result back to x; a = b = 0 evaluates 0 once and binds a, then b.
- a, b = b, a + b builds the whole right-hand tuple from the OLD values before binding a and b; two separate statements would use the new a in the second line.
- Reading a name that is not in the table raises NameError and the program stops at that line.
Default run (10 steps): A 4-line program. Python first compiles all of it, then runs it top to bottom. The variable table on the right starts empty. … Program finished. Final table: x = 16.
Simplified: The programs are fixed templates: you choose the template, its numbers, the operator and the variable name, never the code. The lab's own small Python tokenizer and parser (written for the statements these programs use: assignments, print calls, if / elif / else, for, while, def and return) checks the text; nothing is executed as real Python. Every program the lab can show was compiled by CPython 3.12 and the lab reproduces its exception type, message, line, caret columns and the printed report. A small interpreter written for the same statements then runs them (integers of any size, the str/float results these templates produce, and print); each program was also run in CPython 3.12 under sys.settrace, and the lab reproduces the variables after every line, the output and the error.
Loading the simulation…
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
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.
- 1First 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.
- 2Second assignment
The right side produces the value 10. The name score already exists, so Python updates that existing name-value relationship.
- 3Third 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.
- 1Python first evaluates the expression on the right side of the equals sign.
- 2That evaluation produces a value.
- 3Python creates a variable with the name on the left, or updates that name if it already exists.
- 4Python 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. |
Both examples use the same assignment mechanism. The difference is how much meaning the variable name provides while someone traces the name-value relationship.
Mistakes in Assignment Tracing
Common mistake Reading the equals sign as a statement that two sides are permanently equal.
What it looks likescore = 5 followed later by score = 10Why it failsIn an assignment statement, the equals sign means assign or bind. The later statement updates the existing name-value relationship.
Do this insteadRead the right side as the value-producing side and the left side as the name being created or updated.
Common mistake Assuming Python assigns the left side before evaluating the right side.
What it looks liketotal = price + taxWhy it failsPython evaluates the right side first, producing a value before binding the variable name.
Do this insteadTrace the expression on the right first, then record the binding under the name on the left.
Common mistake Assuming every assignment creates a new variable.
What it looks likescore = 5 followed by score = 10Why it failsThe second statement uses a name that already exists, so it updates that existing variable.
Do this insteadCheck whether the left-side name is new or already present in the current program state.
Common mistake Choosing a name without considering the value it refers to.
What it looks likex = 25 when total = 25 would identify the value's role more clearlyWhy it failsThe name-value relationship still works, but the shorter name gives less information to someone reading or tracing the program.
Do this insteadPrefer 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
- 1A variable is a name that refers to a value.
- 2The assignment form is
variable_name= value. - 3The equals sign in assignment means assign or bind, and data flows from the right side to the left-side name.
- 4Python evaluates the right side first, then creates or updates the variable named on the left.
- 5Clearer variable names make name-value relationships easier to identify while tracing a program.
At a glance
- 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.