CSC108H1 Chap.2 Variables, Assignment and the Memory Model
Variables, Assignment and the Memory Model
The model behind every trace
A variable stores the memory address of a value, not the value itself, and an assignment statement runs right side first: evaluate the expression, then store the address of its value.
The course draws this as a memory model with variables on the left and values, each with an id, a type and the value, on the right.
Why it matters later
Reassigning a name gives it a new address and changes nothing else, so variables are never tied together.
That single rule explains the swap problem in Week 1, function calls in Week 2 and aliasing of lists in Week 5, and Assignment 1 asked for code that reproduces a given memory model diagram.
How the model is tested
Worksheets 2 and 3 give short snippets and ask for the state of memory afterwards, including a check of a two-line swap that a generative AI tool suggested.
Assignment 1 reversed the direction, supplying finished diagrams with ints, strings and Booleans and asking for the assignment statements that produce them. In both directions the skill is the same: every new value gets a new id, and every variable shows only the id it holds now.
What this chapter covers
- 01
Variable naming rules and pothole_case
- 02
Assignment as evaluate then store
- 03
Memory addresses and the id function
- 04
Drawing a memory model diagram
- 05
Reassignment and why variables are not linked
- 06
Swapping two values with a temporary variable
Worked example · free
Draw the memory model for a price change
- 1Create id1 for 12 and id2 for 3, stored in price and qty.
- 1Evaluate 12 * 3 to make 36 at id3 and store id3 in total.
- 2Make 15 at id4 and store id4 in price, leaving total at id3.
Key terms
- Memory Address
- The location in memory where a value is stored, shown in course diagrams as id1, id2 and so on.
- Memory Model
- A diagram of variables and the values their stored addresses lead to.
- Assignment Statement
- A statement that evaluates its right side and stores the resulting address in the variable on the left.
- Reassignment
- Storing a new address in a variable that already exists, which leaves every other variable unchanged.
Variables, Assignment and the Memory Model FAQ
Why does the course say a variable refers to a value?
Because the variable holds the address where the value is stored, not the value itself. Two variables can hold the same address, and reassigning one gives it a new address without disturbing the other, which is exactly what memory model questions test.
Is count = count + 1 a valid statement?
Yes. Python evaluates the right side first using the current value of count, makes a new value one larger, and only then stores its address in count. As algebra it is nonsense, but as an instruction it is a standard update.
Why does a = b followed by b = a fail to swap two values?
After the first line both names hold the same address, so the original value of a has no name left. The second line copies that shared address back and changes nothing. A third variable must keep the first value reachable.
What does the id function show?
It returns the real memory address of a value, a long number that differs between computers and runs. Course diagrams replace it with short labels such as id1 and id2. If two variables give the same id, they refer to the very same value.
Can a variable name start with a number?
No. A name must begin with a letter or an underscore and may contain only letters, digits and underscores, so 2nd_place is rejected while place_2 is fine. Names are case-sensitive, and course style joins lowercase words with underscores, which it calls pothole_case.
Exam move
Draw memory models for five short programs of three or four assignments each, including one reassignment and one statement that copies a variable. Then reverse the task: given a finished diagram, write the code that produces it, the shape Assignment 1 used. Check every drawing in the Python visualizer only after finishing it on paper. Then explain one of your diagrams aloud in two sentences, using refers to rather than is.
Working through Variables, Assignment and the Memory Model in CSC108H1? Sia is AskSia’s AI Computer Science tutor — ask any CSC108H1 Variables, Assignment and the Memory Model question and get a clear, step-by-step explanation grounded in how CSC108H1 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.