Think of a variable like a backpack or a toy box where you can store things! In Python, variables help us store information that we want to use later.
Just like you might put your favorite toy in a box and label it "My Favorite Toy", in Python we can put a number or word in a variable and give it a name!
Imagine if you had to remember every single number in your head while doing math homework. That would be really hard! Variables help the computer remember things for us.
Creating a variable is super easy! You just need:
age = 10 name = "Alex" score = 100
Variables are like pets - you can name them almost anything, but there are some rules:
ā Good Names:
my_age (uses underscore)player_score (descriptive!)favorite_number (easy to understand)ā Bad Names:
2cool (can't start with a number!)my-age (no dashes allowed, use underscores instead)for (this is a special Python word, don't use it!)Pro Tip: Use names that describe what's inside! score is better than x.
Python is like a super-powered calculator! It can work with two types of numbers:
1. Integers (Whole Numbers)
These are numbers without decimal points:
apples = 5 cookies = 12 age = 8
2. Floats (Decimal Numbers)
These are numbers with decimal points:
price = 9.99 height = 4.5 temperature = 98.6
Python can do all kinds of math! Here are the operators:
Addition (+)
total = 5 + 3 # equals 8
Subtraction (-)
remaining = 10 - 4 # equals 6
Multiplication (*)
double = 6 * 2 # equals 12
Division (/)
half = 8 / 2 # equals 4.0
Exponents ()**
squared = 5 ** 2 # equals 25 (5 times 5)
# You start with some points points = 100 # You defeat an enemy and earn 50 points points = points + 50 # now points = 150 # You find a treasure chest worth 200 points! points = points + 200 # now points = 350 # Print your final score print("Your total score is:", points)
Variables can change! That's why they're called "variables" (they vary!).
hearts = 3 print("Hearts:", hearts) # Shows 3 hearts = hearts - 1 # You got hit! Lost a heart print("Hearts:", hearts) # Shows 2 hearts = hearts + 1 # Found a heart! Got it back print("Hearts:", hearts) # Shows 3
Python has shortcuts for updating variables:
score = 10 # These all do the same thing: score = score + 5 score += 5 # Shortcut! # Works for other operations too: score -= 2 # Subtract 2 score *= 3 # Multiply by 3 score /= 2 # Divide by 2
You can create as many variables as you need!
# Shopping at a toy store toy_car = 5 action_figure = 8 board_game = 15 # Calculate total cost total = toy_car + action_figure + board_game print("Total cost: $", total) # Shows 28
# Planning your birthday party! friends_invited = 10 pizzas_ordered = 3 slices_per_pizza = 8 # Calculate total slices total_slices = pizzas_ordered * slices_per_pizza print("Total pizza slices:", total_slices) # How many slices does each friend get? slices_per_friend = total_slices / friends_invited print("Each friend gets:", slices_per_friend, "slices")
Challenge 1: Your Pet's Age
Create a variable for your pet's age (or make one up!), then calculate how old they'll be in 5 years.
Challenge 2: Piggy Bank Math
You have $20 in your piggy bank. You earn $5 for doing chores, then spend $8 on a toy. How much do you have left?
Challenge 3: Sports Statistics
You scored 12 points in the first game, 8 in the second, and 15 in the third. What's your total score?
print() to see what's stored in a variableNow it's your turn! Let's practice what you've learned.
Sign in to track your progress