Everyone Can Learn Program
中文 Start learning
Course 2 · Python BasicsLesson 3 of 14Free to read

Variables · Python from Zero

How does a program remember things?

Course contents

Why 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 = 10

reads "let score refer to 10". The = here means store, not "is equal to". That's why this line makes sense:

score = score + 5

Python 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 yourself

Click the trace steps to watch the value of score change.

score = 0
score = score + 10
score = score + 5
print(score)
Step by step
  1. L1Create the variable: score refers to 0.score = 0
  2. L2Right side first: 0 + 10 = 10. Then score refers to 10.score = 10
  3. L3Again: 10 + 5 = 15.score = 15
  4. L4print shows the current value: 15.output: 15

Copying a value

Try it yourself

After b = a, changing a does not change b.

a = 3
b = a
a = 5
print(a, b)
Step by step
  1. L1a refers to 3.a = 3
  2. L2b gets the current value of a: 3.a = 3, b = 3
  3. L3a now refers to 5; b still refers to 3.a = 5, b = 3
  4. L4Output: 5 3output: 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)
  1. A
    7
  2. B
    9
  3. C
    2
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)
  1. A
    10
  2. B
    1
  3. C
    11
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.

  1. PredictFollow the value

    Type exactly what this program prints.

  2. ModifyAdd a discount

    Add a variable discount with the value 5. Then make total the price × count minus the discount, and print total (it should be 31).

  3. WriteRectangle

    Create width = 7 and height = 4. Store the area in area and the perimeter in perimeter, 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

TermChineseDefinition
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".