←Previous page

02TYPES + LISTS

Every box keeps something.

Picture the computer's memory as a huge shelf full of little boxes. A variable is one box with a name label on it.

WHAT GOES IN A BOX?

A box can keep different kinds of things.

Whole numbers

age = 9 keeps a number you can count and add.

Decimal numbers

height = 1.32 keeps a number with a point in it.

Text

name = "Ana" keeps letters. Text always goes between quotes.

Yes or no

is_raining = True keeps True or False. Only two choices!

age9
height1.32
name"Ana"
is_rainingTrue

Four boxes, four labels. Python finds each value by its name.

A BOX CAN CHANGE

Put in something new, and the old thing leaves.

score = 0→score = 5→
score5

A box keeps one thing at a time. The newest value wins.

A BOX WITH SPACES INSIDE

A list keeps many things in order.

snacks = ["apple", "cookie", "grapes"] puts three things in one box, each in its own little space.

snacks=
  1. "apple"0
  2. "cookie"1
  3. "grapes"2

Python starts counting at 0. snacks[0] is the first space.

Fun fact: In math, a row of numbers in order, like [3, 1, 4], is called a vector. In Python, we make it with a list.

A SHELF OF ROWS

A matrix has rows and columns.

board = [[1, 2, 3], [4, 5, 6]] is a list of lists: two rows with three spaces each, like an egg carton.

board=
01201231456

board[1][0] means row 1, then space 0. That is 4.

RUN IT

Open the snack box.

Guess first. Then run real Python and check.

1

Guess the result

snacks = ["apple", "cookie", "grapes"] print(snacks[0])

What will Python show?

boxes.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: Change 0 to 2. Which snack comes out? Then add your favorite snack to the list.

YOUR TURN

Look inside the boxes.

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

0Completed
7Not tried
0Review
01

Find the text box

PyBot wrote 9 in three different ways.

Which box keeps text?

02

What kind of thing?

is_happy = True

What does this box keep?

03

Count the spaces

colors = ["red", "blue", "green"]

How many spaces does this list have?

04

Count the rows

board = [["X", "O"], ["O", "X"]]

How many rows does this matrix have?

05

Find the decimal

PyBot measured nine and a half centimeters.

Which box keeps a decimal number?

06

Fix PyBot's code

PyBot wants the grape. Remember: the first space is number 0.

Goal output
grape
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

A variable is a labeled box in memory.

It can keep a number, some text, a yes or no, or even a whole list.