University of Toronto · FACULTY OF COMPUTER SCIENCE

CSC108H1 Chap.2 Variables, Assignment and the Memory Model

- one subject, every graph, every model, every mark
6 Chapters3-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 2 of 10 · CSC108H1

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.

In this chapter

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

Q [4 marks]. AskSia-authored practice. After price = 12, qty = 3, total = price * qty and price = 15, draw the memory model and state what total refers to. The marks shown are an AskSia study allocation, not an official University marking scheme.
Figure 2.1.Figure 2.1. After running price = 12, qty = 3, total = price * qty and then price = 15, each arrow is an address stored in a variable. Only price moved to the new value 15; total still refers to 36, computed once from the old price, and the value 12 waits unreferenced.VariablesValuespriceqtytotalid1 int 12id2 int 3id3 int 36id4 int 15
Figure 2.1. After running price = 12, qty = 3, total = price * qty and then price = 15, each arrow is an address stored in a variable. Only price moved to the new value 15; total still refers to 36, computed once from the old price, and the value 12 waits unreferenced.
  • 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.
Four values exist: 12 at id1, 3 at id2, 36 at id3 and 15 at id4. price now holds id4, qty holds id2 and total still holds id3, so total refers to 36. Reassigning price never recomputes total, and 12 is left with nothing referring to it.
Sia tip — Write the address beside each variable rather than the value; a diagram that puts 15 inside the price box hides the fact that a new value was created.
Glossary

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.
FAQ

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.

Study strategy

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.

A+Everything unlocked
Unlocks this Bible + all 21 of your University of Toronto subjects - and 1,000+ Bibles across every Australian university.
Sia - your CSC108H1 tutor, unlimited, worked the way the exam marks it
The full 3-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works