Variables & Data Types

📦 Labeled Boxes: Storing Information in Your Program

Master the fundamental concept: Variables are like labeled boxes that store values

🧠 Mental Model: Variables as Labeled Boxes

The Perfect Metaphor

Think of variables as labeled storage boxes in a warehouse. Each box has:

  • A label (the variable name) - tells you what's inside
  • Contents (the value) - what you actually stored
  • The ability to change - you can empty the box and put something else in
player_name
"Alice"
player_score
1500
game_over
False

Your First Variable Assignment

Let's create a variable! The = symbol means "put this value in that box."

player_name = "Alice"  # Put "Alice" in the box labeled "player_name"
print(player_name)      # Look inside the box and show what's there
Click 'Run This Code' to see the output

⚠️ Critical Pitfall: Assignment Direction

Most Common Beginner Error!

The = in programming is NOT like math! It means "take the value on the RIGHT and put it in the box on the LEFT."

✅ Correct Understanding

x = 5

"Put the value 5 into the box labeled x"

❌ Wrong Thinking

x = 5

"x and 5 are equal" (like math)

Test Your Understanding

What happens when we run this code? Think step by step!

x = 10
y = x
x = 20
print(y)

What will y be?

📋 The Four Essential Data Types

Just like different types of items need different types of containers, different kinds of data need different data types. Python has four essential types you must master:

🔢 int (Integers)

Whole numbers - countable things

age = 25
score = -50
lives = 3

Use for: counting, scoring, indexing

🔢 float (Decimals)

Numbers with decimal points - measurements

price = 19.99
temperature = -5.3
pi = 3.14159

Use for: money, measurements, scientific data

📝 str (Strings)

Text - anything in quotes

name = "Alice"
message = 'Hello!'
address = "123 Main St"

Use for: names, messages, addresses

✅ bool (Boolean)

True or False - yes/no questions

is_raining = True
game_over = False
has_key = True

Use for: flags, conditions, states

🧠 Memory Trick

  • int = Integer = Indivisible (whole numbers)
  • float = Floating point = Has a decimal point that "floats"
  • str = String = A "string" of characters tied together
  • bool = Boolean = Like a switch - only True or False

🔍 Checking and Converting Types

Interactive Type Explorer

Use type() to see what type a variable is:

Test a Value:

Result:

Enter a value and click 'Check Type'

⚠️ Type Conversion Pitfall

Critical mistake: Forgetting that input from users is ALWAYS a string!

❌ This Breaks!

age = input("Age: ")  # "25" (string!)
next_year = age + 1   # ERROR!

✅ This Works!

age = int(input("Age: "))  # Convert!
next_year = age + 1        # Works!

📝 Variable Naming: Rules & Best Practices

📋 The Rules (Must Follow)

  • Start with letter or underscore: name, _count
  • Can contain letters, numbers, underscores: player_1
  • Cannot start with number: 1st_player
  • Cannot use Python keywords: if, for
  • Case sensitive: Namename

⭐ Best Practices (Should Follow)

  • Use descriptive names: player_score not ps
  • Use snake_case: first_name
  • Avoid confusing names: not list or print
  • Be consistent in style
  • Make names meaningful to others

Variable Name Validator

🎯 Mastery Check: Variables & Types

Question 1: Mental Model

What is the best metaphor for understanding variables?

Labeled boxes that can store different types of items

Mathematical equations where both sides are always equal

Permanent storage that never changes once set

Question 2: Assignment Direction

After running this code, what is the value of b?

a = 5
b = a
a = 10

5

10

Error

🎯 Ready for Text Operations!

What You've Mastered

  • ✅ Variables as labeled boxes mental model
  • ✅ Assignment direction (right to left)
  • ✅ Four essential data types (int, float, str, bool)
  • ✅ Type checking and conversion
  • ✅ Variable naming rules and best practices

Now that you can store data, let's learn how to work with text - one of the most important data types for user interaction!

Continue to Working with Text →

🔮 A Glimpse Into the Future

The "labeled box" metaphor you've learned works perfectly for simple data types like numbers and strings. But when we get to lists and other mutable data structures, you'll discover why our mental model needs to evolve to something even more sophisticated. This is the beautiful journey of learning programming - each concept builds upon the previous ones! 🚀