Skip to content

Variables & Data Types πŸš€

Mentor's Note: Variables are the "memory" of your program. Without them, your code would forget everything as soon as it happened! πŸ’‘


🌟 The Scenario: The Warehouse πŸ“¦

Imagine you are managing a huge warehouse.

  • The Logic: You have thousands of items. To stay organized, you put items into Boxes and stick a Label on them (like "Toys" or "Shoes"). πŸ“¦
  • The Result: When you need the toys, you just look for the label "Toys." In Python, the Label is the Variable Name, and the Item inside is the Value. βœ…

πŸ“– Concept Explanation

1. Variables

A variable is a name that refers to a location in computer memory.

2. Rules for Naming Labels 🏷️

  • Must start with a letter or underscore _.
  • Cannot start with a number.
  • Can only contain A-z, 0-9, and _.
  • Case-sensitive (price is different from Price).

3. Dynamic Typing (The "Smart Box")

In Python, boxes are smart. You don't have to decide if a box is only for "Shoes" forever. Today it can hold a number 5, and tomorrow it can hold the word "Hello".


🎨 Visual Logic: The Data Type Tree

mindmap
    root((Python Types))
        Numeric
            int
            float
            complex
        Sequence
            str
            list
            tuple
        Mapping
            dict
        Set
            set
        Boolean
            True
            False

πŸ’» Implementation: Variable Lab

# πŸ›’ Scenario: Stocking the Warehouse
# πŸš€ Action: Creating different types of data

# Integer (Whole number) πŸ”’
box_count = 100 

# Float (Decimal) πŸ’²
price = 19.99 

# String (Text) πŸ“›
label = "Super Gadget" 

# Boolean (True/False) βœ…
is_in_stock = True 

# List (Ordered items) πŸ›οΈ
colors = ["Red", "Blue", "Green"]

# Dictionary (Label: Value pairs) πŸ“–
stats = {"weight": "2kg", "height": "10cm"}

print(f"Item: {label}, Total Boxes: {box_count}")

πŸ“Š Sample Dry Run

Step Instruction Variable Name Type Value
1 age = 25 age int 25 πŸ“¦
2 age = "Old" age str "Old" πŸ”„

πŸ“‰ Technical Analysis

  • Memory: Python uses References. When you create x = 5, Python creates an object 5 in memory and points x to it.
  • Mutability: Some types (like list) can be changed inside their box. Others (like tuple or str) cannotβ€”you have to replace the whole box!

🎯 Practice Lab πŸ§ͺ

Task: Store Profile

Task: Create variables for a store: store_name, rating (decimal), and is_open. Print them all in a single sentence. Hint: Use an f-string: f"Welcome to {store_name}!" πŸ’‘


πŸ“š Best Practices

  • Use Snake Case: Always name variables with underscores (e.g., user_account, not userAccount).
  • Meaningful Names: Use price_per_item, not p.

πŸ’‘ Pro Tip: "Before software can be reusable it first has to be usable." - Ralph Johnson


← Back: Keywords | Next: Numbers & Casting β†’