In this tutorial, we’ll explore a real-world use-case for Python dictionaries by creating a simple contact book application. This contact book will allow you to add, update, remove, and search for contact details such as name, phone number, and email.
Table of Contents
- Getting Started
- Add Contact
- Update Contact
- Remove Contact
- Search Contact
- List All Contacts
- Putting It All Together
1. Getting Started
Let’s start by initializing an empty dictionary that will serve as our contact book.
contact_book = {}
2. Add Contact
We’ll create a function to add a new contact to our contact book. Each contact will have a name, phone number, and email.
def add_contact(name, phone, email):
contact_book[name] = {"phone": phone, "email": email}
print(f"Added contact {name}.")
Example:
add_contact("John", "123-456-7890", "john@email.com")
3. Update Contact
To update existing contact details, let’s create another function.
def update_contact(name, phone=None, email=None):
if name in contact_book:
if phone:
contact_book[name]['phone'] = phone
if email:
contact_book[name]['email'] = email
print(f"Updated contact {name}.")
else:
print(f"Contact {name} does not exist.")
Example:
update_contact("John", phone="098-765-4321")
4. Remove Contact
We’ll also need a function to remove a contact from our contact book.
def remove_contact(name):
if name in contact_book:
del contact_book[name]
print(f"Removed contact {name}.")
else:
print(f"Contact {name} does not exist.")
Example:
remove_contact("John")
5. Search Contact
Creating a function to search for a contact’s details will also be useful.
def search_contact(name):
if name in contact_book:
print(f"Contact {name}: {contact_book[name]}")
else:
print(f"Contact {name} does not exist.")
Example:
search_contact("John")
6. List All Contacts
Let’s add a function to list all the contacts in our contact book.
def list_contacts():
print("All contacts:")
for name, details in contact_book.items():
print(f"{name}: {details}")
Example:
list_contacts()
7. Putting It All Together : GitHub Link
Now we can integrate all these functionalities into a complete contact book program.
# Initialize contact book
contact_book = {}
# Add contact
def add_contact(name, phone, email):
contact_book[name] = {"phone": phone, "email": email}
print(f"Added contact {name}.")
# Update contact
def update_contact(name, phone=None, email=None):
if name in contact_book:
if phone:
contact_book[name]['phone'] = phone
if email:
contact_book[name]['email'] = email
print(f"Updated contact {name}.")
else:
print(f"Contact {name} does not exist.")
# Remove contact
def remove_contact(name):
if name in contact_book:
del contact_book[name]
print(f"Removed contact {name}.")
else:
print(f"Contact {name} does not exist.")
# Search contact
def search_contact(name):
if name in contact_book:
print(f"Contact {name}: {contact_book[name]}")
else:
print(f"Contact {name} does not exist.")
# List all contacts
def list_contacts():
print("All contacts:")
for name, details in contact_book.items():
print(f"{name}: {details}")
# Using the functions
add_contact("John", "123-456-7890", "john@email.com")
add_contact("Emily", "987-654-3210", "emily@email.com")
list_contacts()
search_contact("John")
update_contact("John", phone="111-111-1111")
list_contacts()
remove_contact("Emily")
list_contacts()
Output:
Added contact John.
Added contact Emily.
All contacts:
John: {'phone': '123-456-7890', 'email': 'john@email.com'}
Emily: {'phone': '987-654-3210', 'email': 'emily@email.com'}
Contact John: {'phone': '123-456-7890', 'email': 'john@email.com'}
Updated contact John.
All contacts:
John: {'phone': '111-111-1111', 'email': 'john@email.com'}
Emily: {'phone': '987-654-3210', 'email': 'emily@email.com'}
Removed contact Emily.
All contacts:
John: {'phone': '111-111-1111', 'email': 'john@email.com'}
Congratulations! You’ve successfully created a simple but functional contact book using Python dictionaries. This tutorial aimed to showcase the utility of Python dictionaries for organizing and manipulating structured