←Previous page

02MEMORY + VARIABLES

A computer can remember.

Memory gives a program a small place to keep what it needs right now.

FIRST: COMPUTER MEMORY

Think of a worktable and a drawer.

Memory is the worktable

RAM keeps the things a program is using now. The table can be cleared when the program or device stops.

Storage is the drawer

Files and saved progress can stay for later. They are not the same as the program's working memory.

A variable is a labeled spot

robot_color = "orange" puts one value under a useful name.

robot_color="orange"

Use robot_color later, and Python uses the value orange.

THE = SIGN

= puts a value in a box.

Let's go slowly, one small step at a time.

  1. The box's name goes on the left

    First write the name of the box. Then =. Then what goes inside.

    starsLEFT: the box=5RIGHT: what goes in
  2. = means "put in"

    Read stars = 5 like this: stars gets 5. Python puts 5 inside the box.

    stars = 5
    stars5
    Now the box has 5 inside.
  3. A new = replaces the old value

    A box holds one value. The new value goes in and the old one goes out. = never adds.

    stars = 5
    stars5
    stars = 8
    stars58
    The 5 is gone. The box has 8, not 13.
  4. On the right: a value

    A number or a text in quotes goes straight into the box.

    pet = "cat"
    pet"cat"
  5. On the right: an operation

    Python solves the right side first. Only the answer goes into the box.

    total = 3 + 47
    total7
    3 + 4 is 7. The box keeps 7.
  6. On the right: another box

    Python opens the box on the right, copies its value, and puts the copy in the box on the left.

    stars8
    my_stars = stars
    my_stars8
    Both boxes have 8 now. stars keeps its 8 too.

RUN IT

Help PyBot remember its color.

Guess first. Then run real Python and check.

1

Guess the result

robot_color = "orange" print(robot_color)

What will Python show?

memory.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide ♥

Pyodide 314.0.3 runs CPython in your browser with WebAssembly.

Open-source under the Mozilla Public License 2.0. License

Now change one thing: Put your favorite color between the quotes. Run again.

YOUR TURN

Find the name and the value.

Try each tiny question. A wrong answer becomes something to review.

0Completed
10Not tried
0Review
01

The program needs it now

PyBot is using a score while a game is open.

Which place is like the worktable?

02

Find the label

stars = 5

What is the variable's name?

03

Find what it remembers

stars = 5

What value is stored?

04

Put in something new

stars = 5 stars = 8 print(stars)

What will Python show?

05

Pick a helpful name

PyBot wants to remember your age.

Which name helps the most?

06

Left or right?

PyBot wants to keep 10 points.

Which line puts 10 in the box points?

07

Math on the right

total = 3 + 4 print(total)

What will Python show?

08

Copy a box

a = 6 b = a print(b)

What will Python show?

09

Fix PyBot's code

PyBot misspelled its box's name. Fix it so Python finds the box.

Goal output
cat
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

A variable is a name for a remembered value.

The name helps people and Python find the value again.