Memory and References
A variable is a named location in your computer's memory where you store information.
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.
Variables let a program store, reuse, and change values without rewriting literal constants throughout the program.
| What you write | What it provides |
|---|---|
| A literal such as 42 | The fixed value 42 written directly in the code |
| A variable name | A named handle for accessing a stored value |
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.
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
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.
Following score from Name to Value
Trace the conceptual result of assigning 95 to score and then using the name score.
Create the binding: Python reserves a memory location, stores 95 there, and binds the name score to that location.
Use the name: When the program uses score, Python looks up the name, follows its binding to the location, and retrieves 95.
Interpret the result: The name score is not the literal digits 95 themselves. It is the handle used to access the stored value.
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?
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.
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
Treating a variable name as if it were permanently attached to its first value.
A new assignment changes what the variable name refers to.
Fix:
Trace the most recent assignment when determining a variable's current value.Confusing a literal with a variable.
42 is a fixed literal written directly in the code, while a variable name is a handle for accessing stored information.
Fix:
Separate the value itself from the name used to retrieve a stored value.Assuming that two variables with equal values must always refer to the same memory location.
Python may reuse a location in some cases or use different locations containing equal values.
Fix:
Focus first on the practical fact that both names provide access to the value 10.
Practice the Trace
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.
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.
- A variable is a named location in computer memory used to store information.
- A literal constant writes a fixed value directly, while a variable supplies a name for accessing stored information.
- Using a variable name causes Python to look up its binding and retrieve the associated value.
- Reassignment changes the value reached through the name; after score changes from 95 to 87, score refers to 87.
- 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.