Skip to content
KodeTrail

Camp 2 · Step 5 of 23

Variables: naming your data

Teach Python to remember values by giving them names — the single most important idea in programming.

12 min+50 XP

So far our programs compute things and immediately forget them. Variables fix that: they let you store a value under a name and use it later. If programming had only one big idea, this would be it.

Creating a variable

The = sign means "store the right side under the name on the left":

Python

Read name = "Ada" as "name becomes Ada" — not as "equals" from math class. Watch how values move into their boxes:

Memory lab0/3
name = "Ada"
steps = 4200
steps = steps + 800

Variables in memory

Nothing stored yet — step through the code.

That last step is the famous one: steps = steps + 800 looks impossible as math, but as "take the current value of steps, add 800, store it back" it makes perfect sense.

Naming rules (and taste)

Python's hard rules for names:

  • Letters, digits, and underscores only: total_steps, camp2
  • Can't start with a digit: 2camp
  • No spaces: use snake_case instead
  • Case matters: Steps and steps are different variables

And one rule of taste that matters more than all of those: names should say what they hold. s saves keystrokes today and costs you an hour next week.

Checkpoint

After these two lines, what does print(x) show?

x = 10
x = 3
Checkpoint

Which is a valid Python variable name?

What's next

Variables can hold different types of values — numbers, text, truth. Next: numbers, and all the math Python can do with them.