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):

Python

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):

Python

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:

Python

If someone accidentally writes:

Python

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)

Python

→ If GST changes to 0.20 next budget → change only one line

Example 2: Game settings (super useful)

Python

Example 3: Wrong vs Right (see the difference)

Python

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!)

  1. Open replit.com or any Python editor
  2. Create 4–5 constants (ALL CAPS):
    • TAX_RATE
    • TIP_PERCENT
    • DAYS_IN_MONTH (assume 30)
    • YOUR_FAVORITE_FOOD_PRICE
    • SPEED_LIMIT_HYDERABAD_ROADS
  3. Use them in some simple calculations or prints Example:
Python

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! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *