Chapter 4: Variables
Variables.
Think of this as our classroom session #2 (after understanding what programming itself is). I’m going to explain it slowly, like I’m sitting next to you in a Hyderabad café with a plate of irani chai and bun maska, drawing on a napkin.
No rush. We’ll use tons of real-life analogies, step-by-step examples, common mistakes beginners make, and Python code you can copy-paste right now.
1. What is a Variable? (The napkin drawing version)
A variable is a named storage box in the computer’s memory.
- You give the box a name (like labeling it)
- You put something inside the box (a number, a name, a yes/no, etc.)
- Later, whenever you want that thing, you just say the name — the computer goes to that box and gives you what’s inside
Real-life analogy everyone gets immediately:
Imagine your kitchen shelf in Hyderabad home:
- You have a plastic dabba (container) labeled “Sugar”
- Inside it you keep sugar (the value)
- When you want to make chai, you don’t say “give me that white sweet stuff from the third shelf” — you just say “pass the sugar dabba”
- Tomorrow you can empty it and put salt instead — now the same dabba labeled “Sugar” has salt inside
- The label stayed the same, but the contents changed
That’s exactly what a variable is:
- Label = variable name
- Contents = the value / data
- You can change what’s inside anytime
2. Why do we even need variables? (The big “why”)
Without variables, every time you want to use a number or name, you have to write it again and again — hard-coded.
Bad way (no variables):
|
0 1 2 3 4 5 6 7 8 |
print("Your bill is", 1200 + 1200 * 0.18) # 1416.0 print("After discount", 1416.0 - 100) print("Final amount ₹", 1316.0) |
Good way (with variables):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
bill = 1200 gst_rate = 0.18 discount = 100 gst_amount = bill * gst_rate total_before_discount = bill + gst_amount final_amount = total_before_discount - discount print("Your bill is ₹", total_before_discount) print("After discount ₹", final_amount) |
→ Much clearer, reusable, easy to change (what if GST becomes 0.20 tomorrow? Just change one place!)
3. How do we create & use variables in Python? (Very simple)
In Python (easiest for beginners):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# Syntax: name = value player_name = "Virat Kohli" # string (text) runs_scored = 85 # integer (whole number) strike_rate = 142.5 # float (decimal number) is_man_of_the_match = True # boolean (True / False) wickets = 0 # starts at zero # Now use them anywhere print(player_name, "scored", runs_scored, "runs today!") print("Strike rate was", strike_rate) print("Man of the match?", is_man_of_the_match) |
Output:
|
0 1 2 3 4 5 6 7 8 |
Virat Kohli scored 85 runs today! Strike rate was 142.5 Man of the match? True |
4. Popular Real-Life Analogies Teachers Use (pick your favorite)
| Analogy | What it maps to | Why students like it |
|---|---|---|
| Named Dabba / Jar | Kitchen container with label | Very Indian, relatable in Hyderabad home |
| Hotel Room Number | Room 305 always same, guest changes | Room number = name, guest = value |
| Label on Water Bottle | Bottle label stays, water level changes | You drink → level down, refill → level up |
| Bank Account | Account number fixed, balance changes | Most used in salary / UPI examples |
| Chalkboard / Blackboard | Write name once, change number anytime | Teacher writes “Total = 0”, then updates it |
| House Address | Address fixed, people/furniture inside change | Advanced — good for understanding memory later |
My personal favorite for Hyderabad students → Railway PNR number
- PNR = variable name (fixed unique label)
- Passenger name, train number, seat, status = values that can change
- You always refer by PNR, not by rewriting full details
5. Important Rules & Gotchas Beginners Face (very common in first week)
- Name rules (must follow):
- Starts with letter or underscore (_)
- No spaces → use player_name or playerName
- No special chars except underscore
- Case-sensitive → Age ≠ age
- You can change the value anytime (that’s why “variable”)
|
0 1 2 3 4 5 6 7 8 9 10 |
temperature = 38 print("Now:", temperature) # 38 temperature = 42 # re-assign (change content of box) print("After 2 hours:", temperature) # 42 |
- Assignment goes RIGHT to LEFT
|
0 1 2 3 4 5 6 |
score = 50 + 30 # RIGHT side calculated first → 80, then stored in score |
Wrong way beginners write:
|
0 1 2 3 4 5 6 |
50 + 30 = score # ERROR — cannot assign to expression |
- You must create variable before using it
|
0 1 2 3 4 5 6 7 |
print(age) # NameError: name 'age' is not defined age = 25 |
6. Quick Mini-Examples You Can Try Right Now (copy-paste)
Example 1: Simple personal details
|
0 1 2 3 4 5 6 7 8 9 10 |
your_name = input("What is your name? ") your_city = "Hyderabad" your_age = int(input("Your age? ")) print("Hello", your_name, "from", your_city, "! You are", your_age, "years old.") |
Example 2: Basic shop bill (most useful for beginners)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
item = "Chicken Biryani" price = 280 quantity = 2 gst = 0.05 total = price * quantity gst_amount = total * gst final_bill = total + gst_amount print("You ordered", quantity, item) print("Total before GST ₹", total) print("GST ₹", gst_amount) print("Pay ₹", final_bill) |
Example 3: Change value mid-program (shows why “variable”)
|
0 1 2 3 4 5 6 7 8 9 10 |
marks = 65 print("Current marks:", marks) marks = marks + 10 # add grace marks print("After grace:", marks) |
7. Your 5-Minute Home Task (do it today!)
- Open replit.com or any online Python editor
- Write a program with 4–5 variables:
- your_name
- favorite_food
- monthly_pocket_money
- hours_sleep_last_night
- is_tired (True/False)
- Print a fun sentence using all of them, like: “Hi [name]! You love [food] and get ₹[money]. You slept [hours] hours so you must be [tired?]”
Try it — then paste your code here if you want me to check / improve / explain any error.
Any doubt right now?
- “Why = sign is used?”
- “What is string vs integer?”
- “Can variable name be in Telugu?” (spoiler: yes in Python 3, but not recommended)
- “Show me 5 more examples”
Just shout — I’m right here 😄
Next class (when you’re ready): Data Types (what can we actually put inside these boxes?)
Keep rocking, champ! You’re building real foundation now 🚀
