Lesson · Chapter: Variables+10 XP for reading
What are Variables?
A variable is like a labeled box that stores a value. You give the box a name, and you can put something inside it.
Where you'll use this
Games store your health, score, and level in variables. A quiz stores the right answer. Every app on your phone is, underneath, a big pile of variables changing as you tap.
Prefer to watch? This chapter as a video:
Creating Variables
Use = to store a value in a variable:
python
name = "Alex"
age = 10
Output
Press Run to see the output.
Types of Values
Strings — text inside quotes:
python
color = "blue"
greeting = 'Hello!'
Output
Press Run to see the output.
Integers — whole numbers:
python
score = 100
lives = 3
Output
Press Run to see the output.
Booleans — True or False:
python
is_happy = True
is_raining = False
Output
Press Run to see the output.
Using Variables
You can use variables in print():
python
name = "Alex"
print(name)
Output
Press Run to see the output.
Output:
Alex
You can print variables with text:
python
name = "Alex"
print("Hello,", name)
Output
Press Run to see the output.
Output:
Hello, Alex
Changing Variables
You can change what's inside a variable:
python
score = 0
print(score)
score = 100
print(score)
Output
Press Run to see the output.
Output:
0 100
The old value is replaced by the new one.
Did you know?
Python isn't named after the snake — it's named after Monty Python's Flying Circus, a British comedy show its creator Guido van Rossum was reading scripts of while building the language.
Check yourself
Pause & predict — what will this print?
candy = 5 candy = candy - 2 candy = candy + 1 print(candy)