Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Python
Stage 5: Specialization

Part 04 / Topic 11 of 11

Putting it together: shopping cart

prices = {"rice": 60, "milk": 30, "soap": 45}
categories = {"rice": "grocery", "milk": "dairy", "soap": "personal care"}

cart = ["rice", "milk", "rice", "soap"]

total = sum(prices[item] for item in cart)   # generator expression (see section 7)
print(f"Total: ₹{total}")              # Total: ₹195
print(f"Rice count: {cart.count('rice')}")  # Rice count: 2

unique_categories = {categories[item] for item in cart}
print(sorted(unique_categories))       # ['dairy', 'grocery', 'personal care']

See the full version in examples/06_shopping_cart.py.

Done here? The practice set for Part 4 — 8 exercises with solutions — is on the part page.

Search the guide