Variables · Python from Zero
How does a program remember things?
Course contentsWhy you need it
A game needs to remember your score, a shop needs the price, a form needs your name. Without memory a program could only print fixed things. Variables give values a name so you can store them, use them later, and change them.
The intuition
Think of a variable as a label stuck on a value:
score = 10reads "let score refer to 10". The = here means store, not "is equal to". That's why this line makes sense:
score = score + 5Python first calculates the right side (10 + 5 = 15), then moves the label score to 15. Read assignments right to left: work out the value, then give it the name.
Names can use letters, digits and _, can't start with a digit, can't contain spaces, and capital letters matter (Score ≠ score).
Definition
Variable
A variable is a name that refers to a value stored in the computer's memory.
Assignment
An assignment statement (name = expression) evaluates the expression on the right and binds the result to the name on the left.
Examples
Keeping score
Try it yourselfClick the trace steps to watch the value of score change.
score = 0
score = score + 10
score = score + 5
print(score)- L1Create the variable: score refers to 0.
score = 0 - L2Right side first: 0 + 10 = 10. Then score refers to 10.
score = 10 - L3Again: 10 + 5 = 15.
score = 15 - L4print shows the current value: 15.
output: 15
Copying a value
Try it yourselfAfter b = a, changing a does not change b.
a = 3
b = a
a = 5
print(a, b)- L1a refers to 3.
a = 3 - L2b gets the current value of a: 3.
a = 3, b = 3 - L3a now refers to 5; b still refers to 3.
a = 5, b = 3 - L4Output: 5 3
output: 5 3
Common mistakes
= is not "equals"
x = x + 1 would be nonsense in maths, but in Python it means "add 1 to x". Comparing for equality uses == (later lesson).
Using a variable before creating it
print(total) before any total = ... line is a NameError. Python reads top to bottom.
Quick quiz
1. What does this print?
x = 2
x = x * 3
x = x + 1
print(x)Show the answer
Answer: A
2 × 3 = 6, then 6 + 1 = 7.
2. What does this print?
a = 1
b = a
a = 10
print(b)Show the answer
Answer: B
b got a copy of the value 1 before a changed.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictFollow the value
Type exactly what this program prints.
- ModifyAdd a discount
Add a variable
discountwith the value 5. Then maketotalthe price × count minus the discount, and printtotal(it should be 31). - WriteRectangle
Create
width = 7andheight = 4. Store the area inareaand the perimeter inperimeter, then print area and perimeter on two lines.
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| Variable | 变量 | A name that refers to a value stored in memory. |
| Assignment | 赋值 | A statement that evaluates the right side and binds the result to a name. |
| Value | 值 | A piece of data, such as 42 or "hello". |