1. A price list
A dictionary (dict) stores pairs of key: value, like a price list at a kirana shop:
prices = {"atta": 55, "dal": 120}
prices["dal"]gives 120prices["ghee"] = 650adds a new pair (or changes an old one)prices["salt"]crashes with aKeyErrorif salt is missingprices.get("salt", 0)is the safe way: it gives 0 when salt is missing"dal" in priceschecks whether a key exists
prices = {"atta": 55, "dal": 120}
prices["ghee"] = 650
print(prices)
print(prices["dal"])
print(prices.get("salt", 0))
Challenge: Kirana price checker
First add ghee to prices at ₹650. Then read an item and look it up with .get(item, 0). If the price is more than 0, print dal costs ₹120; otherwise print Sorry, no salt today (with the real item name). (Prices are sample data.)