Variables

Module 2 · Variables · Lesson 4 of 8

How to use variables

Once a box has a value, you use it just by writing its name. Wherever Python sees the name, it swaps in the stored value — so a variable can go anywhere a literal could: inside print, in an expression, even in another assignment.

score = 100
print(score * 2)
200

That's the whole idea: a name stands in for its value. The big win is reuse — set a price once and refer to it ten times, and if it changes, you change it in a single place.

Run it yourselfruns in your browser · Python
main.py

One catch: a variable has to exist before you use it. Ask Python for a name it's never seen and you'll get a NameError — the same error you met from a typo, for the same reason: Python has no box by that name.