In the previous section we discussed the basics of Python Dictionaries. Now let us take a look at some real-life application Problems.
1. Shopping Cart System
Write a Python program to store items with prices and calculate the total bill.
cart = {
"Rice": 60,
"Milk": 25,
"Eggs": 50
}
# Add item
cart["Bread"] = 30
# Calculate total
total = sum(cart.values())
print("Items Purchased:")
for item, price in cart.items():
print(item, "=", price)
print("Total Bill =", total)
# Items Purchased:
# Rice = 60
# Milk = 25
# Eggs = 50
# Bread = 30
# Total Bill = 165
2. Word Frequency Counter
Write a program to count how many times each word appears in a sentence.
sentence = "python is easy and python is powerful"
words = sentence.split()
freq = {}
for word in words:
if word in freq:
freq[word] += 1
else:
freq[word] = 1
print(freq)
# {'python': 2, 'is': 2, 'easy': 1, 'and': 1, 'powerful': 1}
3. Employee Salaries
You are given a dataset of employee salaries. Write a Python program to perform a batch update that applies a 10% performance bonus to every employee. The final output should display the updated salary after the bonus.
Amit: 25000
Divya: 30000
Kiran: 28000
salary = {
"Amit": 25000,
"Divya": 30000,
"Kiran": 28000
}
for emp in salary:
salary[emp] = salary[emp] * 1.10
print("Updated Salaries:")
for emp, sal in salary.items():
print(emp, ":", int(sal))
# Updated Salaries:
# Amit : 27500
# Divya : 33000
# Kiran : 30800
4. Phone Contact Lookup System
Write a Python Program to look for a contact system by typing their names and provide their number if the contact details are found.
contacts = {
"Ravi": "9876543210",
"Smith": "9123456780",
"John": "9988776655",
"Meera": "9845012345",
"Syaan": "7766554433",
"Caroline": "9112233445"
}
name = input("Enter name to search: ")
if name in contacts:
print(f"{name}'s Number: {contacts[name]}")
else:
print("Contact not found")
# Enter name to search: Smith
# Smith's Number: 9123456780
# Enter name to search: Alice
# Contact not found
5. Product Stock Check
Write a Python Program to check the remaining stocks of products after a sale.
inventory = {"Pen": 50, "Notebook": 30, "Eraser": 20}
while True:
item = input("\nEnter the Item sold (or 'done') to finish: ").capitalize()
if item == "Done":
break
if item in inventory:
qty = input(f"Quantity of {item}: ")
if qty.isdigit():
qty = int(qty)
if qty <= inventory[item]:
inventory[item] -= qty
else:
print(f"Only {inventory[item]} available!")
else:
print("Enter a number.")
else:
print("Not in stock.")
print("\nFinal Status:", inventory)
# Enter the Item sold (or 'done') to finish: Pen
# Quantity of Pen: 14
# Enter the Item sold (or 'done') to finish: Notebook
# Quantity of Notebook: 16
# Enter the Item sold (or 'done') to finish: Eraser
# Quantity of Eraser: 3
# Enter the Item sold (or 'done') to finish: done
# Final Status: {'Pen': 36, 'Notebook': 14, 'Eraser': 17}
6. Student Marks Management System
Write a Python program to take students’ names and their marks and display the name with the highest marks.
students = {}
while True:
print("\n1. Add Student 2. Display List 3. Exit")
choice = input("Enter choice (1-3): ")
if choice == '1':
name = input("Enter name: ")
marks = int(input("Enter marks: "))
students[name] = marks
print(f"Added {name} successfully.")
elif choice == '2':
print("\nStored Student List")
for name, marks in students.items():
print(f"Name: {name}, Marks: {marks}")
if students:
topper = max(students, key=students.get)
print(f"Current Topper: {topper}")
elif choice == '3':
print("Exiting program.")
break
else:
print("Invalid choice! Please try again.")
# 1. Add Student 2. Display List 3. Exit
# Enter choice (1-3): 1
# Enter name: Bobby
# Enter marks: 84
# Added Bobby successfully.
# 1. Add Student 2. Display List 3. Exit
# Enter choice (1-3): 1
# Enter name: Mary
# Enter marks: 56
# Added Mary successfully.
# 1. Add Student 2. Display List 3. Exit
# Enter choice (1-3): 2
# Stored Student List
# Name: Bobby, Marks: 84
# Name: Mary, Marks: 56
# Current Topper: Bobby