Imagine a labeled box where you can put something inside and take it out later. That is exactly what a variable does. It holds a value under a name so you do not have to repeat the raw number or text every time.
Creating a variable
# Create a variable called score and store 10
score = 10
print(score)The
You can store different kinds of values:
= sign is assignment. It does not mean "equals" like in math. It means: put the value on the right side into the name on the left side.You can store different kinds of values:
- Numbers without decimals are integers (
int) - Numbers with decimals are floats (
float) - Text is a string (
str) - True or False is a boolean (
bool)
Changing the value
# Start with 5
lives = 5
print(lives)
# Change it to 4 (lost a life!)
lives = 4
print(lives)When you reassign, Python simply updates what that name refers to. The old value is gone unless you saved it somewhere else.
Naming tip: Use
Naming tip: Use
snake_case for multi-word names. That means lowercase letters with underscores between words: player_score, not PlayerScore or playerscore. It reads clearly and follows Python conventions.Checking the type
# See what kind of value a variable holds
age = 25.0
print(type(age))
name = "Ada"
is_adult = True
print(type(name), type(is_adult))