w3resource

How to Build a Contact Book Program in Python


Contact Book:

Build a program that manages contacts with features like adding, editing, and deleting contacts.

Input values:

User interacts with the contact book by adding, editing, and deleting contacts.

Output value:

Visual representation of the contact book with the list of contacts, along with feedback on user actions (addition, editing, deletion).

Example:

Input values:
1. Add a new contact
- User enters contact details: Name: Alinafe Lilith, Email: alinafe@example.com, Phone: 123-456-7890
Output value:
Contact book updates to display the newly added contact "Alinafe Lilith" with the specified details.
Input values:
2. Edit an existing contact
- User selects the contact "Alinafe Lilith" to edit and updates the email to "alinafe.lilith@example.com"
Output value:
The contact book is updated to display the edited contact "Alinafe Lilith" with the updated email.
Input values:
3. Delete a contact
- User selects the contact "Alinafe Lilithe" to delete.
Output value:
Contact book updates to remove "Alinafe Lilith" from the list.
Input values:
4. View the contact details
- User selects a contact to view details.
Output value:
Contact book displays the details of the selected contact, including name, email, and phone number.

Here are two different solutions for implementing a contact book program. One solution uses the dictionary data structure, and the other leverages a class structure to manage the contacts.

Solution 1: Using Dictionary for Contact Management

Code:

# Define a dictionary to store contacts
contacts = {}

# Function to add a new contact
def add_contact(name, email, phone):
    contacts[name] = {'email': email, 'phone': phone}
    print(f"Added contact: {name}")

# Function to edit an existing contact
def edit_contact(name, email=None, phone=None):
    if name in contacts:
        if email:
            contacts[name]['email'] = email
        if phone:
            contacts[name]['phone'] = phone
        print(f"Updated contact: {name}")
    else:
        print("Contact not found.")

# Function to delete a contact
def delete_contact(name):
    if name in contacts:
        del contacts[name]
        print(f"Deleted contact: {name}")
    else:
        print("Contact not found.")

# Function to view contact details
def view_contact(name):
    if name in contacts:
        contact = contacts[name]
        print(f"Name: {name}, Email: {contact['email']}, Phone: {contact['phone']}")
    else:
        print("Contact not found.")

# Example Usage
add_contact("Cliff Lolita", "cliff@example.com", "123-456-7890")
edit_contact("Cliff Lolita", email="cliff.lolita@example.com")
view_contact("Cliff Lolita")
delete_contact("Cliff Lolita") 

Output:

Added contact: Cliff Lolita
Updated contact: Cliff Lolita
Name: Cliff Lolita, Email: cliff.lolita@example.com, Phone: 123-456-7890
Deleted contact: Cliff Lolita 

Explanation:

  • First create an empty contacts dictionary to store contact information.
  • The add_contact function adds a contact with the provided details.
  • The edit_contact function updates existing contact details if the contact exists.
  • The delete_contact function removes a contact if it is found in the dictionary.
  • The view_contact function displays the contact details if the contact exists.

Solution 2: Using Classes for Contact Management

Code:

# Define a Contact class to manage individual contact details
class Contact:
    def __init__(self, name, email, phone):
        self.name = name
        self.email = email
        self.phone = phone

    def update(self, email=None, phone=None):
        if email:
            self.email = email
        if phone:
            self.phone = phone
        print(f"Updated contact: {self.name}")

    def display(self):
        print(f"Name: {self.name}, Email: {self.email}, Phone: {self.phone}")

# Define a ContactBook class to manage the list of contacts
class ContactBook:
    def __init__(self):
        self.contacts = {}

    def add_contact(self, name, email, phone):
        if name in self.contacts:
            print("Contact already exists.")
        else:
            self.contacts[name] = Contact(name, email, phone)
            print(f"Added contact: {name}")

    def edit_contact(self, name, email=None, phone=None):
        if name in self.contacts:
            self.contacts[name].update(email, phone)
        else:
            print("Contact not found.")

    def delete_contact(self, name):
        if name in self.contacts:
            del self.contacts[name]
            print(f"Deleted contact: {name}")
        else:
            print("Contact not found.")

    def view_contact(self, name):
        if name in self.contacts:
            self.contacts[name].display()
        else:
            print("Contact not found.")

# Example Usage
contact_book = ContactBook()
contact_book.add_contact("Polat Erik", "polat@example.com", "123-456-7890")
contact_book.edit_contact("Polat Erik", email="polat.erik@example.com")
contact_book.view_contact("Polat Erik")
contact_book.delete_contact("Polat Erik") 

Output:

Added contact: Polat Erik
Updated contact: Polat Erik
Name: Polat Erik, Email: polat.erik@example.com, Phone: 123-456-7890
Deleted contact: Polat Erik

Explanation:

  • A Contact class is defined to encapsulate contact details and provide methods for updating and displaying them.
  • A ContactBook class manages multiple contacts using a dictionary and provides methods for adding, editing, deleting, and viewing contacts.
  • The add_contact method in ContactBook checks for duplicate contacts before adding.
  • Each Contact object is stored in the contacts dictionary, allowing efficient contact management.


Follow us on Facebook and Twitter for latest update.