Identifiers and Variables
A variable names a memory location rather than being the data itself.
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…
Why a Variable Is Not the Data Itself
A common first idea about variables is that a variable is simply another name for a piece of data. That idea is useful at the start, but it hides an important distinction. In Java, a variable name refers to a location in memory that can hold data. The programmer works with the name, and the computer keeps track of the actual location.
What do you think happens?
Given score = 5; followed later by score = 10;, what value does score hold after the second statement runs?
Reveal answer
10
The assignment score = 10 evaluates the expression on the right, which is 10, and places that result into the location identified by score. The previous contents, 5, are replaced. The name score stays the same while the stored value changes.
The Labeled Container Model
Think of a variable as a labeled container. The label identifies the container, while the data is what the container currently holds. The container stays the same even when its contents are replaced. Because of this, one variable can be associated with different values at different points during a program. The label is the name you write in your code; the container is a location in memory; the contents are the data currently stored there.
Name Versus Stored Value
When the same variable is reassigned, the name does not change. Only the contents of its location change. In the sequence below, the name score appears at every step, but the value it refers to is different each time it is assigned.
5
10
7- The name score is the same in all three statements; it labels one container.
- Each println reads the value currently stored in that container at the moment it runs.
- Each assignment replaces the previous contents rather than adding to them.
The variable therefore plays two different roles. On the left side of an assignment, it identifies the location that will receive a result. Inside an expression on the right side, it contributes the value currently stored in that location. Keeping these two roles separate is the key to tracing any program that reuses variables.
How Java Runs an Assignment
An assignment is an instruction for the computer to carry out. Its general form is a variable, followed by an equals sign, followed by an expression, and then a semicolon. Execution follows a specific direction. First, Java evaluates the expression on the right. Then it places the resulting data value into the location identified by the variable on the left. If that location already contains data, the new result replaces what was there before.
Notice that the left-hand variable is not read during this process. It identifies where the result goes. The values that feed the computation come from variables inside the expression on the right.
Reading Values From Variables in an Expression
When an expression contains variables, each variable supplies the value currently stored in its location at the moment the expression is evaluated. The example below follows an interest calculation with three variables. The source material uses the same kind of trace: when interest is wrong, check whether rate had the intended value, whether principal had the intended value when the expression was evaluated, and whether the result was stored in interest.
Tracing interest through an assignment
Trace the values of principal, rate, and interest as the statements run in order.
- 1Set the inputs
principal is given the value 200 and rate is given the value 5. Each location now holds its data.
- 2Evaluate the expression
interest = principal * rate reads the current value of principal (200) and the current value of rate (5). The expression produces 1000.
- 3Store the result
The value 1000 is placed into the location for interest, which previously held nothing useful.
- 4Change an input
rate = 10 replaces the contents of rate. interest still holds 1000, because nothing has re-run the assignment.
- 5Reassign interest
interest = principal * rate now reads principal (200) and rate (10), producing 2000, which replaces the old contents of interest.
interest holds 1000 after the first assignment and 2000 after the second. The name interest never changed; its stored value did.
1000
1000
2000- The first println shows 1000 because rate was 5 when the expression was evaluated.
- Changing rate later does not change interest by itself; the assignment must run again.
- The third assignment reads the current value of rate, which is now 10.
Why x = x + 1 Is Not an Equation
The equals sign in an assignment looks like the equals sign in a mathematical equation, but the two have different meanings. In Java, an assignment tells the computer to perform an operation at a particular time. It does not claim that the two sides will remain equal forever. A mathematical equation x = x + 1 would be false for every number x, but the assignment x = x + 1 is a valid instruction that is carried out in a specific order: read x, add 1, store the result back into x.
int x = 3;x holds 3
x = x + 1;x holds 4
Read as an equation, the statement would demand that x equal x plus one, which is impossible. Read as an assignment, it is a sequence of two actions. This is why the statement produces a different stored value each time it runs.
Mistakes When Reading Assignments
Common mistake Treating a variable as just another name for a fixed piece of data
What it looks likeBelieving that score always means 5 because it started as 5.
Why it failsThe location is the container; the data inside it can be replaced by later assignments.
Do this insteadAsk what value the location currently holds at the exact line you are tracing.
Common mistake Reading x = x + 1 as a statement that is always false
What it looks likeConcluding that the line cannot be correct because x cannot equal x + 1.
Why it failsIn Java the equals sign is an instruction to evaluate the right side and store the result, not a claim that both sides stay equal.
Do this insteadRead the line in order: evaluate the right side with the current value, then store the result in the left variable.
Common mistake Assuming changing an input variable updates a result that was already computed
What it looks likeChanging rate to 10 and expecting interest to change immediately.
Why it failsThe expression was evaluated when the assignment ran; interest kept the value it received then.
Do this insteadRun the assignment again after changing an input, so the expression reads the new value.
Common mistake Confusing the variable on the left with a value that is read during evaluation
Practice Tracing Assignments
Trace the following statements. After each line, write down the value currently stored in count and in step. Then predict the final printed value of count.
Hints
- Evaluate the right-hand side using the values stored at that moment, not the values you expect later.
- The name count stays the same; only its contents change.
- A line such as count = count + step reads count before it stores the new value.
7- The first assignment reads count as 0 and step as 2, storing 2 in count.
- step becomes 5, but count is not updated until the next assignment runs.
- The second assignment reads count as 2 and step as 5, storing 7 in count.
Key Takeaways for Tracing Variables
- 1A variable name refers to a location in memory that holds data; the name is a fixed label, and the stored value can change.
- 2An assignment evaluates the expression on the right first, then stores the resulting value in the variable on the left, replacing any previous contents.
- 3Variables inside an expression supply their current stored values at the moment the expression is evaluated.
- 4An assignment such as x = x + 1 is an instruction executed in order, not a mathematical equation that must always hold.
At a glance
- A variable name refers to a memory location that holds data, so the name stays fixed while the stored value can change.
- Java evaluates the right-hand expression first and then stores the result in the variable on the left.
- Variables inside an expression contribute their current stored values when the expression is evaluated.
- An assignment such as x = x + 1 is an instruction executed in sequence, not a permanent mathematical equation.