Dictionary

Tutorial for Python Dictionary: Building a Simple Contact Book

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

  1. Getting Started
  2. Add Contact
  3. Update Contact
  4. Remove Contact
  5. Search Contact
  6. List All Contacts
  7. Putting It All Together

1. Getting Started

Let’s start by initializing an empty dictionary that will serve as our contact book.

Python
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.

Python
def add_contact(name, phone, email):
    contact_book[name] = {"phone": phone, "email": email}
    print(f"Added contact {name}.")

Example:

Python
add_contact("John", "123-456-7890", "john@email.com")

3. Update Contact

To update existing contact details, let’s create another function.

Python
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:

Python
update_contact("John", phone="098-765-4321")

4. Remove Contact

We’ll also need a function to remove a contact from our contact book.

Python
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:

Python
remove_contact("John")

5. Search Contact

Creating a function to search for a contact’s details will also be useful.

Python
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:

Python
search_contact("John")

6. List All Contacts

Let’s add a function to list all the contacts in our contact book.

Python
def list_contacts():
    print("All contacts:")
    for name, details in contact_book.items():
        print(f"{name}: {details}")

Example:

Python
list_contacts()

Now we can integrate all these functionalities into a complete contact book program.

Python
# 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