- Courses
- 💻Programming Languages
- 🐍Python
- Core Data Structures
- Guided Exercise: Build a Contact Book
On this page
Guided Exercise: Build a Contact Book
Overview
This guided exercise closes Module 3 — Core Data Structures by requiring you to combine everything you learned about lists, tuples, dictionaries, sets, and nested structures.
You will build a fully functional contact book, a classic real‑world application that stores, retrieves, updates, and displays structured information.
The goal is to design a clean, maintainable data model and iterate over complex data confidently.
1. Requirements
Your contact book must:
- Store multiple contacts in a list
- Represent each contact as a dictionary
- Include at least: name, phone, email, and tags
- Allow adding new contacts
- Allow searching contacts by name
- Allow listing all contacts in a readable format
- Use nested structures where appropriate (e.g., tags as a set or list)
- Use iteration to process and display data
This ensures you apply every major concept from Module 3.
2. Designing the Data Model
A contact book is naturally represented as a list of dictionaries.
Each contact is a dictionary:
{
"name": "Alice Johnson",
"phone": "555‑1234",
"email": "[email protected]",
"tags": {"friend", "gym"} # a set for uniqueness
}
The entire contact book is a list:
contacts = []
This structure is flexible, readable, and scalable.
3. Adding a New Contact
You will collect user input, build a dictionary, and append it to the list.
def add_contact(contacts):
name = input("Name: ")
phone = input("Phone: ")
email = input("Email: ")
tags = input("Tags (comma‑separated): ").split(",")
contact = {
"name": name,
"phone": phone,
"email": email,
"tags": {t.strip() for t in tags}
}
contacts.append(contact)
print("Contact added.")
This demonstrates dictionary creation, list mutation, and set construction.
4. Searching for a Contact
Searching requires iterating through the list and checking each dictionary.
def search_contact(contacts, name):
for contact in contacts:
if contact["name"].lower() == name.lower():
return contact
return None
This uses iteration, membership logic, and dictionary access.
5. Displaying a Contact
Readable formatting is essential for real applications.
def display_contact(contact):
print("Name:", contact["name"])
print("Phone:", contact["phone"])
print("Email:", contact["email"])
print("Tags:", ", ".join(contact["tags"]))
This demonstrates iteration over a set and string manipulation.
6. Listing All Contacts
Iterate through the list and display each dictionary.
def list_contacts(contacts):
if not contacts:
print("No contacts found.")
return
for contact in contacts:
print("—" * 20)
display_contact(contact)
This reinforces nested iteration and structured output.
7. Putting It All Together
Below is a complete, minimal implementation that satisfies all requirements.
def add_contact(contacts):
name = input("Name: ")
phone = input("Phone: ")
email = input("Email: ")
tags = input("Tags (comma‑separated): ").split(",")
contact = {
"name": name,
"phone": phone,
"email": email,
"tags": {t.strip() for t in tags}
}
contacts.append(contact)
print("Contact added.")
def search_contact(contacts, name):
for contact in contacts:
if contact["name"].lower() == name.lower():
return contact
return None
def display_contact(contact):
print("Name:", contact["name"])
print("Phone:", contact["phone"])
print("Email:", contact["email"])
print("Tags:", ", ".join(contact["tags"]))
def list_contacts(contacts):
if not contacts:
print("No contacts found.")
return
for contact in contacts:
print("—" * 20)
display_contact(contact)
def main():
contacts = []
while True:
print("\n1. Add Contact")
print("2. Search Contact")
print("3. List All Contacts")
print("4. Quit")
choice = input("Choose an option: ")
if choice == "1":
add_contact(contacts)
elif choice == "2":
name = input("Enter name to search: ")
result = search_contact(contacts, name)
if result:
display_contact(result)
else:
print("Contact not found.")
elif choice == "3":
list_contacts(contacts)
elif choice == "4":
break
else:
print("Invalid option.")
This program demonstrates:
- Lists for storing multiple records
- Dictionaries for structured data
- Sets for unique tags
- Nested structures
- Iteration over complex data
- Clean, maintainable design
8. What You Should Learn from This Exercise
By completing this project, you demonstrate mastery of:
- Modeling real‑world data with nested structures
- Iterating through lists, dictionaries, sets, and mixed types
- Building maintainable, structured Python programs
This exercise completes your foundation in Python’s core data structures and prepares you for more advanced modules involving algorithms, file handling, and object‑oriented programming.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Iterating Over Complex Data
Next
Functions and Modular Programming
- Overview
- 1. Requirements
- 2. Designing the Data Model
- 3. Adding a New Contact
- 4. Searching for a Contact
- 5. Displaying a Contact
- 6. Listing All Contacts
- 7. Putting It All Together
- 8. What You Should Learn from This Exercise