Module 2 Lesson 11: Hands-on Projects
Put your data structure and algorithm skills to work. Build a Contact Book, a Grade Tracker, and an Inventory System.
Lesson 11: Module 2 Hands-on Projects
Now that you’ve mastered Lists, Dictionaries, and searching/sorting algorithms, it’s time to see how they work together to build practical tools. In this lesson, we will build three data-focused applications.
Project 1: Contact Book (Dictionary-based)
This project uses a Dictionary to store contact information and allow for instant lookups.
contacts = {}
while True:
print("\n--- Contact Book ---")
print("1. Add Contact")
print("2. Search Contact")
print("3. Exit")
choice = input("Select an option: ")
if choice == "1":
name = input("Enter name: ")
phone = input("Enter phone number: ")
contacts[name] = phone
print("Contact added!")
elif choice == "2":
name = input("Enter name to search for: ")
print(f"Phone Number: {contacts.get(name, 'Contact not found.')}")
elif choice == "3":
break
Project 2: Student Grade Tracker (List of Dicts)
In this project, we’ll use a List of Dictionaries and learn how to perform calculations across them.
students = [
{"name": "Alex", "grade": 85},
{"name": "Sara", "grade": 92},
{"name": "Bill", "grade": 78}
]
# Calculate Class Average
total = 0
for student in students:
total += student["grade"]
average = total / len(students)
print(f"Class Average: {average:.2f}")
# Find the Top Student
top_student = sorted(students, key=lambda x: x["grade"], reverse=True)[0]
print(f"Top Student: {top_student['name']} with {top_student['grade']}%")
Project 3: Simple Inventory System (Set-based)
This project uses Sets to track unique items in a warehouse.
inventory = {"Laptop", "Mouse", "Monitor"}
sold_items = {"Mouse", "Keyboard"}
# 1. What unique items have been sold that were NOT in inventory?
alert_items = sold_items - inventory
print(f"Alert: Sold items not in stock: {alert_items}")
# 2. What items are currently in stock?
inventory.add("Keyboard")
inventory.remove("Monitor")
print(f"Updated Stock: {inventory}")
Part 2: Module 2 Wrap-up Exercises
Exercise 1: Dictionary to List
Given a dictionary {"A": 1, "B": 2, "C": 3}, write a script that converts it into a list of tuples: [("A", 1), ("B", 2), ("C", 3)].
Exercise 2: Unique Counter
Ask the user to type in a paragraph of text. Write a script that counts how many unique words are in that paragraph.
Module 2 Quiz
1. Which data structure is best for ensuring no duplicates? A) List B) Tuple C) Set D) Dictionary
2. What is the time complexity of Binary Search? A) O(1) B) O(n) C) O(log n) D) O(n²)
3. If you need to store data as Key: Value pairs, what do you use?
A) Set
B) Tuple
C) Dictionary
D) List
4. True or False: Converting a List to a Set is a common way to remove duplicates.
5. Which sorting method modifies the original list directly?
A) sorted(my_list)
B) my_list.sort()
Quiz Answers
- C | 2. C | 3. C | 4. True | 5. B
Conclusion
You’ve completed Module 2: Data Structures and Algorithms! You now know how to organize data and how to process it efficiently.
In Module 3: Functions and Modular Programming, we’ll learn how to wrap our code into reusable components so we don't have to keep typing the same logic over and over!