🐍 Day 2: Variables

Storing and Using Data

📚 What You'll Learn Today

Today you'll master the art of storing data in variables:

📦 What is a Variable?

Think of a variable as a labeled box where you store information. You can put something in, take it out, or change what's inside.

Creating a Variable:

name = "Alex" age = 25 city = "New York"
💡 Analogy: The box is named "name" and it contains "Alex". The box is named "age" and it contains 25.

Using Variables:

print("Hello, my name is " + name) print("I am " + age + years old")
Hello, my name is Alex I am 25 years old

🔢 Data Types

Python has several built-in data types. Here are the most common:

Data Type Name Example Description
String str "Hello" Text data
Integer int 42 Whole numbers
Float float 3.14 Decimal numbers
Boolean bool True / False True or False

Examples:

# String - text in quotes greeting = "Hello, World!" email = "alex@example.com" # Integer - whole numbers year = 2026 score = 100 # Float - decimal numbers price = 19.99 pi = 3.14159 # Boolean - True or False is_student = True has_completed = False

📝 Naming Rules

Variable names in Python must follow these rules:

Good Names:

user_name = "Alex" email_address = "alex@example.com" age_in_years = 25 total_score = 1500

Bad Names (will cause errors):

2nd_place = 2 # Can't start with number class = 5 # "class" is a keyword my-variable = "text" # Can't use hyphen

🔄 Changing Values

You can change a variable's value at any time:

score = 10 print(score) # Output: 10 score = 20 print(score) # Output: 20 score = score + 5 # Add 5 to existing value print(score) # Output: 25
💡 Shortcut: You can use += to add: score += 5 means score = score + 5

🏋️ Exercise: Create Your Profile

Create variables for a user profile and print them out:

# Create these variables # Your name (string) # Your age (integer) # Whether you like Python (boolean) # Then print them like: # print("Name: " + your_name) # print("Age: " + str(your_age))

Bonus Challenge:

Calculate and print what your age will be in 10 years!

# Example solution: name = "Alex" age = 25 likes_python = True print("Name:", name) print("Age:", age) print("Likes Python:", likes_python) print("Age in 10 years:", age + 10)

🎭 Interactive Demo: Story Generator

Ready to see variables in action? Let's build a personalized story generator that uses all the variable types we just learned!

🚀 What You'll Build:

  • Collect user input (strings, numbers)
  • Store data in different variable types
  • Calculate new values using variables
  • Generate a personalized story with f-strings
  • Create magic spells based on user choices
🎭 Try the Story Generator Demo

See how variables come together to create something fun!

💡 What This Demo Teaches:
  • How to use input() to get user data (strings)
  • Converting strings to numbers with int() and float()
  • Boolean logic for yes/no questions
  • Math operations with variables
  • Using f-strings to combine variables in text

✅ Key Takeaways

🎉 Ready for Day 3?

Tomorrow you'll learn about conditions and loops - making decisions in your code!

Marking complete will unlock Day 3!