Concepts / Memory and References

Memory and References

A variable is a named location in your computer's memory where you store information.

  • Programming
Interactive lab

Try it: Names and Objects

How Python variables are names bound to objects: assignment never copies, mutating a list is seen through every name that points at it, and ints are replaced rather than changed.

How it works

  1. name = object binds a name; b = a makes b point at the same object.
  2. Mutating a list (append, +=) changes the one shared object.
  3. a = a + [x] and y = y + 1 create NEW objects and re-point one name.
  4. Passing a list to a function passes the reference, so the function can change it.

Default run (7 steps): Two names, one list. Every name is a label that points at an object. … Printed: [1, 2, 3] / True

Simplified: Six fixed scripts on a tiny heap; you choose the values. Object numbers are illustrative, not real id() values.

Educational simulation

Loading the simulation…

Why Names Matter

A program often needs to remember information and use that information again later. Writing a literal value such as 42 or 'hello' gives the program a value directly, but it does not give that value a name you can refer to elsewhere. A variable provides that name. In the conceptual model used here, a variable is a named location in computer memory where information is stored.

Key point

Variables let a program store, reuse, and change values without rewriting literal constants throughout the program.

What you writeWhat it provides
A literal such as 42The fixed value 42 written directly in the code
A variable nameA named handle for accessing a stored value

A literal provides a value directly; a variable provides a name through which a stored value can be accessed.

Name-to-Value Mapping

When you create a variable in Python, the conceptual process has three parts. Python reserves a location in memory, stores the assigned value at that location, and creates a binding between the variable name and the location. Later, when the program uses the variable name, Python looks up the name, finds the location it refers to, and retrieves the value stored there.

scorevariable nameMemory location95stored valuerefers toholds
How does a variable name connect to the value stored in a location in memory?

The diagram uses a conceptual memory location rather than a specific physical address. Its purpose is to show the relationship: score is the name, the location is where the value is stored, and 95 is the value retrieved when the program uses score.

Assignment as Binding

Definition

In Python, assignment uses a single equals sign. The general form is variable_name = value. The name appears on the left, the equals sign is in the middle, and the value or an expression that produces a value appears on the right.

Worked example

Following score from Name to Value

Trace the conceptual result of assigning 95 to score and then using the name score.

  1. 1
    Create the binding

    Python reserves a memory location, stores 95 there, and binds the name score to that location.

  2. 2
    Use the name

    When the program uses score, Python looks up the name, follows its binding to the location, and retrieves 95.

  3. 3
    Interpret the result

    The name score is not the literal digits 95 themselves. It is the handle used to access the stored value.

Result

Using score gives access to the stored value 95.

Reassignment Changes the Reference

What do you think happens?

Suppose score first refers to 95 and is then assigned 87. What should score refer to after the second assignment?

  • 95

  • 87

  • Both 95 and 87 through the name score

Reveal answer
Answer:

87

After reassignment, the name score refers to the new value. The old value is no longer directly accessible through score.

A variable's value can change while a program is running. Conceptually, before reassignment, score is bound to a location holding 95. After assigning 87, score is bound to the new value. Python may store that new value at a different memory location rather than erasing the old value and writing the new value in exactly the same place. The important result is that the name score now refers to 87, and the old value is no longer accessible through score.

scorebefore95stored valuescoreafter87stored valuerefers torefers to
What happens to a variable's stored value when the program assigns it a new value?
Best practice

When debugging an unexpected value, trace where the variable was assigned and reassigned. The name may be correct while its current binding is different from the one you expected.

Shared Values and Separate Names

More than one variable can be assigned the same value. For example, x can be assigned 10 and y can also be assigned 10. From a practical programming perspective, both variables hold the value 10, so either name can be used to access that value in the program.

Mistakes with Variables

  • Common mistake

    Treating a variable name as if it were permanently attached to its first value.

    What it looks like

    Assuming that score will always refer to 95 after it has later been assigned 87.

    Why it fails

    A new assignment changes what the variable name refers to.

    Do this instead

    Trace the most recent assignment when determining a variable's current value.

  • Common mistake

    Confusing a literal with a variable.

    What it looks like

    Treating 42 and a name that refers to the value 42 as identical concepts.

    Why it fails

    42 is a fixed literal written directly in the code, while a variable name is a handle for accessing stored information.

    Do this instead

    Separate the value itself from the name used to retrieve a stored value.

  • Common mistake

    Assuming that two variables with equal values must always refer to the same memory location.

    What it looks like

    Concluding that x and y must have the same location merely because both hold 10.

    Why it fails

    Python may reuse a location in some cases or use different locations containing equal values.

    Do this instead

    Focus first on the practical fact that both names provide access to the value 10.

Practice the Trace

EASY

Imagine the following sequence: a variable named temperature is assigned the value 20, and later it is assigned the value 25. Describe the conceptual state before and after the second assignment. Identify the name, the first value, the second value, and which value temperature refers to at the end.

Hints
  • Start by identifying the name bound during the first assignment.
  • Then describe what changes when the second value is assigned.
  • The final answer should distinguish the variable name from the value it currently accesses.
Key point

A complete trace should say that temperature first refers to 20, then is rebound so that it refers to 25. The name remains temperature, but the value reached through that name changes.

Practical Meaning

Variables make programs flexible because information can be stored once, reused in multiple places, and changed as execution continues. They can represent user input, calculation results, configuration settings, and other information a program needs to manage. Thinking in terms of names, bindings, memory locations, and values also gives you a way to debug: when a variable is not what you expect, trace the assignments that determined its current binding.

  1. 1
    A variable is a named location in computer memory used to store information.
  2. 2
    A literal constant writes a fixed value directly, while a variable supplies a name for accessing stored information.
  3. 3
    Using a variable name causes Python to look up its binding and retrieve the associated value.
  4. 4
    Reassignment changes the value reached through the name; after score changes from 95 to 87, score refers to 87.
  5. 5
    Tracing variable assignments helps explain program behavior and debug unexpected values.

Key Takeaways

  • Variables provide named access to stored information.
  • The variable name is bound to a memory location, and using the name retrieves the value associated with that binding.
  • A new assignment can rebind the same name to a new value.
  • Variables allow programs to reuse and change information instead of repeating fixed literals.
  • The conceptual name-to-value model is useful for tracing and debugging programs.

Related concepts

More concepts starting with M