๐ฆ 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".
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.
The animation could not start because the D3 library did not load. Check your internet connection and reload the page. The notes below still work.
A variable is a labelled box in memory. biryani_price = 250 stores 250 in the box named biryani_price. The = means "store".
Ravi orders 1 Biryani, 2 Butter Naan and 1 Lassi. Meena writes the bill in two stages: # Data, then # Data Operation.
a = 250 โฆ e = 50 works, but the names hide what is inside. Only Meena remembers which box is naan.
Typing b instead of c gives โน750 instead of โน390. Python shows no error.
naan_price = 40 and naan_qty = 2 say what they hold. Anyone can read the file.
The formula is price ร quantity for each dish, added up. It reads like English and prints Total bill: โน 390.
Biryani goes up to โน280: change only biryani_price = 280. The bill becomes โน420 by itself.
Use letters, digits and _ only. No digit at the start, no spaces, no keywords. Names are case-sensitive. Use snake_case.
Both programs print 390. Names are for humans: Python doesn't care, your team does.
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.
# Data
a = 250
b = 220
c = 40
d = 60
e = 50
# Data Operation
t = a + c * 2 + d
print("Total:", t)
Output
Total: 390
# 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
# 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