Kids Learn AI Logo
Lesson 3: Variables and Numbers ✨
Learn how to store values in variables and work with numbers. Discover math operations, variable naming rules, and fun real-world examples!

šŸ“¦ Variables and Numbers

What Are Variables?

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!

šŸŽÆ Why Do We Need Variables?

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 Your First Variable

Creating a variable is super easy! You just need:

  1. A name for your variable (like naming a pet!)
  2. An equals sign (=)
  3. A value to store (a number, word, or other data)
age = 10 name = "Alex" score = 100

šŸ“› Rules for Naming Variables

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.

šŸ”¢ Working with Numbers

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

🧮 Math Operations in Python

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)

šŸŽ® Fun Example: Video Game Score

# 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)

šŸ”„ Updating Variables

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

⚔ Shortcut Operators

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

šŸŽØ Using Multiple Variables

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

🌟 Real-World Example: Birthday Party

# 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")

šŸŽÆ Practice Challenges

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?

šŸ’” Did You Know?

  • The equals sign (=) in programming doesn't mean "equals" like in math. It means "store this value"!
  • You can have thousands of variables in a single program!
  • Good programmers use descriptive variable names to make code easy to read

šŸŽ“ Key Takeaways

  1. Variables store information for later use
  2. Use descriptive names for your variables
  3. Variables can hold numbers (integers and floats)
  4. You can do math with variables (+, -, *, /, **)
  5. Variables can change their values anytime
  6. Use print() to see what's stored in a variable

šŸš€ Ready to Code?

Now it's your turn! Let's practice what you've learned.

Sign in to track your progress

Lesson not completed
Python Code Editor
Press Ctrl+Enter to run code
1
2
3
4
5
6
7
8
9
10
11
Output:
Loading Python environment...