Python variables: why good names matter

Charminar Kitchen, Hyderabad. Meena the cashier writes a bill program twice: first with names a, b, c, d, e, then with names that say what is inside. Watch the boxes fill, the wrong bill appear, and the right bill come out.

1

Step-by-step notes

1

๐Ÿ“ฆ What is a variable

A variable is a labelled box in memory. biryani_price = 250 stores 250 in the box named biryani_price. The = means "store".

2

๐Ÿ› The menu and the order

Ravi orders 1 Biryani, 2 Butter Naan and 1 Lassi. Meena writes the bill in two stages: # Data, then # Data Operation.

3

๐Ÿ˜• # Data with bad names

a = 250 โ€ฆ e = 50 works, but the names hide what is inside. Only Meena remembers which box is naan.

4

๐Ÿ˜ก # Data Operation with bad names

Typing b instead of c gives โ‚น750 instead of โ‚น390. Python shows no error.

5

๐Ÿ™‚ # Data with good names

naan_price = 40 and naan_qty = 2 say what they hold. Anyone can read the file.

6

๐Ÿ˜€ # Data Operation with good names

The formula is price ร— quantity for each dish, added up. It reads like English and prints Total bill: โ‚น 390.

7

โœ๏ธ Change one line

Biryani goes up to โ‚น280: change only biryani_price = 280. The bill becomes โ‚น420 by itself.

8

๐Ÿ“ Naming rules

Use letters, digits and _ only. No digit at the start, no spaces, no keywords. Names are case-sensitive. Use snake_case.

9

๐Ÿค Bad names vs good names

Both programs print 390. Names are for humans: Python doesn't care, your team does.

10

๐Ÿงฉ 3 steps on the real order

1 Data (what do I have?), 2 Operation (what do I do with it?), 3 Design (how do I pack it?). Function: 1 task. Class: several functions sharing the same data.

The two programs

โŒ bad_names.py

# Data
a = 250
b = 220
c = 40
d = 60
e = 50

# Data Operation
t = a + c * 2 + d
print("Total:", t)

Output

Total: 390

โœ… good_names.py

# Data
biryani_price = 250
paneer_price = 220
naan_price = 40
lassi_price = 60
gulab_jamun_price = 50
biryani_qty = 1
naan_qty = 2
lassi_qty = 1

# Data Operation
bill_total = (biryani_price * biryani_qty
              + naan_price * naan_qty
              + lassi_price * lassi_qty)
print("Total bill: โ‚น", bill_total)

Output

Total bill: โ‚น 390

The 3-step program: Data โ†’ Operation โ†’ Design

๐Ÿงฉ three_steps.py

# 1. Data
biryani_price = 250
menu = {"biryani": 250, "paneer": 220, "naan": 40,
        "lassi": 60, "gulab_jamun": 50}
order = {"biryani": 1, "naan": 2, "lassi": 1}
is_member = True

# 2. Operation
total = 0
for item, qty in order.items():             # Repeat
    if item in menu:                        # Check
        total = total + menu[item] * qty    # Calculate
print("Total:", total)
print("Naan on the menu:", "naan" in menu)        # Check
print("Free gulab jamun:", total > 300)           # Compare
print("Member offer:", is_member and total > 300) # Combine

# 3. Design: Function
def bill_total(menu, order):
    total = 0
    for item, qty in order.items():
        total = total + menu[item] * qty
    return total

print(bill_total(menu, order))

# 3. Design: Class
class RestaurantBill:
    def __init__(self, menu, order):
        self.menu = menu
        self.order = order

    def total(self):
        return bill_total(self.menu, self.order)

    def gst(self, rate=0.05):
        return self.total() * rate

    def final_amount(self):
        return self.total() + self.gst()

bill = RestaurantBill(menu, order)
print(bill.total(), bill.gst(), bill.final_amount())

Output

Total: 390
Naan on the menu: True
Free gulab jamun: True
Member offer: True
390
390 19.5 409.5