Variables - Your Storage Boxes
Variables are like labeled storage boxes in your kitchen. You might have a box labeled 'Sugar' (holding sugar) and another labeled 'Flour' (holding flour). In Python, you create boxes (variables) to store information.
A variable is a named container that holds data in your computer's memory. Think of it as a sticky note attached to a value – you write a name on the note, and that name always refers to the same value (unless you change it). The equals sign `=` is called the assignment operator; it stores whatever is on the right into the variable on the left.
**Rules for variable names:**
- Must start with a letter (a-z, A-Z) or underscore `_`
- Can contain letters, numbers, and underscores (but cannot start with a number)
- Are case-sensitive (`age` and `Age` are different variables)
- Cannot be a Python keyword (like `if`, `for`, `while`, `print`, etc.)
- Use descriptive names: `student_age` instead of `a`
**Data types in variables:**
Variables can hold different types of data:
- `int` – whole numbers (e.g., `25`)
- `float` – decimal numbers (e.g., `5.9`)
- `str` – text in quotes (e.g., `"Raj"`)
- `bool` – True or False (capital T and F)
**Reassigning variables:**
You can change what a variable holds at any time by assigning a new value. The old value is lost.
**Multiple variables in one line:**
Python allows you to assign multiple variables in a single line: `x, y, z = 1, 2, 3`
**Common beginner mistakes:**
1. Using a variable before assigning a value – causes `NameError`
2. Forgetting quotes for strings – Python thinks it's a variable name
3. Using spaces in variable names – use underscores instead
4. Confusing `=` (assignment) with `==` (equality check)
**Why variables matter:**
Variables are the foundation of any program – they store user input, calculation results, configuration settings, and everything else your program needs to remember. Mastering variables means mastering data flow in your code.
**Practice ideas:**
- Create variables for your name, age, city, and favorite hobby. Print them in a sentence.
- Swap the values of two variables (e.g., `a` and `b`) without using a third variable (hint: `a, b = b, a`).
- Try assigning a number to a variable, then reassign it to a string – notice that Python doesn't complain (dynamic typing).
# ========== EXAMPLE 1: Creating and Using Variables ==========
print("=== Example 1: Basic Variable Creation ===")
name = "Raj" # string (text)
age = 25 # integer (whole number)
height = 5.9 # float (decimal)
is_happy = True # boolean (True/False)
print("Name:", name)
print("Age:", age)
print("Height:", height, "feet")
print("Happy?", is_happy)
print() # empty line
# ========== EXAMPLE 2: Reassigning Variables ==========
print("=== Example 2: Changing Variable Values ===")
score = 100
print("Initial score:", score)
score = 95 # reassign new value
print("After reassignment:", score)
score = score - 10 # update using current value
print("After subtracting 10:", score)
print()
# ========== EXAMPLE 3: Multiple Variables & Dynamic Typing ==========
print("=== Example 3: Multiple Variables and Dynamic Types ===")
# Assign multiple variables in one line
a, b, c = 10, 20, 30
print(f"a={a}, b={b}, c={c}")
# Swap two variables easily
a, b = b, a
print(f"After swapping: a={a}, b={b}")
# Variables can change type dynamically
value = 42
print(f"value is {value}, type: {type(value).__name__}")
value = "Now I'm text"
print(f"value is {value}, type: {type(value).__name__}")
→ Run this code interactively