Storage

Learn about Steps before learning this page.

Variables

Programs often store things, and then get them for use later. Say we wanted to put socks into one drawer, and shoes in another drawer.

Then we want to see what is in the drawers. The print() steps will show what is in the drawers. Both drawer1 and drawer2 below are called "variables". It is a fancy name meaning that they can change and hold different things at different times.

The equal sign is not like what you may have seen in math. It will be explained later.

The drawer1 can be set to socks, and then set to elephant. Notice that the socks are erased, so the program does not know anything about socks when the steps are completed.

Equal Sign

In programming, someone chose the equal sign to mean something about moving things from one storage place to another. Someone chose a key from the keyboard to mean this. They could have used another key, or even a word.

It might have been more clear to type:

   drawer1 <- 'socks'".

In real life, things mostly get moved from one place to another. In programming, the equal sign really means copy.

Things on the right side of the equal sign get copied to the left side, and the left side doesn't change.

The code above shows that the equal sign copied drawer2 to drawer1. The equal sign read from drawer2, and did not change it, so it still contains 'shoes'.

Numbers

The drawers can be renamed and the results are the same.

The box and drawer variables can also hold numbers.

Lists

A list is something that holds many variables, but are accessed using numbers. So the list is like drawers in a dresser. The print steps can show what all drawers contain.

The first print before setting the socks and shoes shows "[0, 0, 0]" Wait a minute. After running this, the second print shows "[0, 'socks', 'shoes']" Why is the first item 0?

Most programming languages like Python count from 0.

Now the last drawer in the dresser is 0.

Replacing something in the list is the same as replacing in a single variable.