Chapter 5: Constants
Constants slowly, with the same Hyderabad-style analogies, lots of examples, and Python code you can try immediately.
I’ll compare them directly to variables (since you already know those) so it clicks fast.
1. What is a Constant? (The simple napkin explanation)
A constant is a named storage box (just like a variable) … but once you put something inside it, you promise never to change it again during the entire program.
- Variable = box whose content you can change anytime (like temperature during the day)
- Constant = box whose content is fixed forever after first putting it in (like the number of days in a week = 7)
In real life:
- Variable: Your bank balance → changes every time you spend or get salary
- Constant: Number of planets in our solar system (for most programs) → we treat it as 8, never change it
The whole point of constants → make code clearer, safer, and easier to maintain.
2. Why do we need constants? (The real “why”)
Imagine you write a program to calculate area of circle many times.
Bad way (using magic numbers everywhere):
|
0 1 2 3 4 5 6 7 8 9 10 |
area1 = 3.14 * 5 * 5 area2 = 3.14 * 12 * 12 area3 = 3.14 * 8.5 * 8.5 print(area1, area2, area3) |
Problems:
- What is 3.14? Magic number — nobody knows immediately
- Tomorrow scientist says “use more precise 3.14159” → you have to find & change every single place → easy to miss one → bugs!
- If someone reads your code → confusion
Good way (with constant):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
PI = 3.14159 area1 = PI * 5 * 5 area2 = PI * 12 * 12 area3 = PI * 8.5 * 8.5 print(area1, area2, area3) |
Now:
- Clear what PI means
- Change value in one place only → everywhere updates
- Code reads like English: “multiply by PI”
3. How Python handles constants (very important for you)
Python does NOT enforce constants like C, Java, JavaScript (no const keyword that prevents change).
In Python → constants are just a convention (team rule):
- Write constant names in ALL CAPITAL LETTERS with underscores
- Everyone agrees: “We never change ALL_CAPS names”
So:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Good constant style (everyone understands this) PI = 3.1415926535 DAYS_IN_WEEK = 7 GST_RATE = 0.18 MAX_LOGIN_ATTEMPTS = 5 APP_VERSION = "1.2.3" HYDERABAD_PIN_CODE_EXAMPLE = 500081 # These should NEVER be changed later in code |
If someone accidentally writes:
|
0 1 2 3 4 5 6 |
PI = 22/7 # ← bad! Don't do this |
Python allows it (no error), but good programmers never do it — it’s like breaking a promise.
(Advanced note: Python 3.11+ has typing.Final to hint at constants better, but beginners → just use ALL_CAPS.)
4. Everyday Hyderabad-style analogies for constants
| Analogy | Maps to constant because… | Example value |
|---|---|---|
| Number of days in a week | Never changes in normal programs | 7 |
| GST rate in Telangana (2026) | Fixed by government till changed officially | 0.18 or 0.05 for food |
| Speed of light (in code) | Physics constant — same everywhere | 299792458 m/s |
| Paradise Biryani secret spice code | Fixed recipe — never change or taste changes! | “PARADISE_001” |
| Number of overs in IPL T20 | Rule of game — fixed unless rule changes | 20 |
| Your home PIN code (for forms) | Doesn’t change unless you move | 500081 |
5. Real useful examples you can copy-paste
Example 1: Shop bill with GST (very common)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
GST_RATE = 0.18 SERVICE_CHARGE = 0.05 item_price = 450 quantity = 2 subtotal = item_price * quantity gst_amount = subtotal * GST_RATE service_amount = subtotal * SERVICE_CHARGE total_bill = subtotal + gst_amount + service_amount print("Subtotal ₹", subtotal) print("GST (18%) ₹", gst_amount) print("Service charge (5%) ₹", service_amount) print("Pay total ₹", total_bill) |
→ If GST changes to 0.20 next budget → change only one line
Example 2: Game settings (super useful)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
MAX_LIVES = 3 SCREEN_WIDTH = 1920 SCREEN_HEIGHT = 1080 FPS = 60 GRAVITY = 9.81 print("Game starts with", MAX_LIVES, "lives") |
Example 3: Wrong vs Right (see the difference)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# Bad - magic numbers, hard to understand & change discount = 1200 * 0.10 # what is 0.10? final = 1200 - discount # Good - clear constants DISCOUNT_RATE = 0.10 original_price = 1200 discount_amount = original_price * DISCOUNT_RATE final_price = original_price - discount_amount print("You saved ₹", discount_amount) |
6. Constants vs Variables – Quick Comparison Table
| Feature | Variable | Constant (Python style) |
|---|---|---|
| Name convention | snake_case or camelCase | ALL_CAPS_WITH_UNDERSCORES |
| Can change value? | Yes, anytime | Should never change |
| Purpose | Data that changes (score, name…) | Fixed values (PI, rates, settings…) |
| Example | score = 85; score += 10 | PI = 3.14159 (don’t touch again) |
| Why use? | Track changing state | Clarity, easy updates, fewer bugs |
7. Your 5-Minute Mini Task (try now!)
- Open replit.com or any Python editor
- Create 4–5 constants (ALL CAPS):
- TAX_RATE
- TIP_PERCENT
- DAYS_IN_MONTH (assume 30)
- YOUR_FAVORITE_FOOD_PRICE
- SPEED_LIMIT_HYDERABAD_ROADS
- Use them in some simple calculations or prints Example:
|
0 1 2 3 4 5 6 7 8 9 |
TAX_RATE = 0.18 price = 500 tax = price * TAX_RATE print("Price ₹", price, "| Tax ₹", tax, "| Total ₹", price + tax) |
Paste your code here if you want feedback / corrections 😄
Any doubt right now?
- “Why ALL CAPS only in Python?”
- “What if I accidentally change a constant?”
- “Show constants in a small bill splitter program”
- “Next topic please” (maybe data types?)
Just tell me — no question is small here.
You’re building strong basics, keep going champ! 🚀
