Ravindra BagaleCourses & study guides मराठी Track your progress

Python Mini Projects for Beginners: 5 Practice Projects Before Learning pandas

P13.1 Python Project 1: Shopping Bill Calculator

Why. Every shop needs a bill: items, quantities, prices, tax and a total. It uses variables, dictionaries, loops, functions and f-strings.

What. We store a cart as a list of dictionaries, calculate line totals, add GST and print a neat bill.

How.

Build the bill

  1. Write the cart as data.
  2. Write a function for one line total.
  3. Loop over the cart and print each line.
  4. Add GST and print the grand total.

Worked example A — Step 1: the cart and line totals.

Step 1 — What we do. A customer at a store in Pune buys three items.

Step 2 — Type and run this code.

cart = [
    {"item": "Rice 5kg", "qty": 1, "price": 425},
    {"item": "Milk 1L", "qty": 4, "price": 56},
    {"item": "Biscuits", "qty": 3, "price": 30},
]
def line_total(row):
    return row["qty"] * row["price"]
for row in cart:
    print(row["item"], line_total(row))

Step 3 — Output you should see.

Rice 5kg 425
Milk 1L 224
Biscuits 90

Step 4 — What it means. Each line total is qty × price.

Worked example B — Step 2: the full bill with GST.

Step 1 — What we do. We add a subtotal, 5% GST and formatting.

Step 2 — Type and run this code.

cart = [
    {"item": "Rice 5kg", "qty": 1, "price": 425},
    {"item": "Milk 1L", "qty": 4, "price": 56},
    {"item": "Biscuits", "qty": 3, "price": 30},
]
def line_total(row):
    return row["qty"] * row["price"]
subtotal = 0
for row in cart:
    t = line_total(row)
    subtotal += t
    print(f"{row['item']:<12} {row['qty']:>3} x {row['price']:>5} = ₹{t:>7,.2f}")
gst = subtotal * 0.05
print("-" * 36)
print(f"{'Subtotal':<26} ₹{subtotal:>7,.2f}")
print(f"{'GST 5%':<26} ₹{gst:>7,.2f}")
print(f"{'Total':<26} ₹{subtotal + gst:>7,.2f}")

Step 3 — Output you should see.

Rice 5kg       1 x   425 = ₹ 425.00
Milk 1L        4 x    56 = ₹ 224.00
Biscuits       3 x    30 = ₹  90.00
------------------------------------
Subtotal                   ₹ 739.00
GST 5%                     ₹  36.95
Total                      ₹ 775.95

Step 4 — What it means. :<12 pads text to the left, :>7 aligns numbers to the right — a tidy printed bill.

Ravindra Bagale's Tip

Build projects in small steps. Make step 1 work and print it before adding step 2.