Python

 

Introduction

A contact book is a digital application used to store, retrieve, update, and delete contact information. This simple Python application allows you to manage your contacts by providing features to add, view, and remove contact entries.

The objective of this program is to give users an easy way to maintain their contact list, leveraging Python’s simplicity and versatility. The program stores each contact’s name, phone number, and email address, offering essential functionalities to manage them effectively.

Program Code

# Contact Book Application in Python

class ContactBook:
    def __init__(self):
        self.contacts = {}

    def add_contact(self, name, phone, email):
        self.contacts[name] = {'Phone': phone, 'Email': email}
        print(f"Contact for {name} added successfully!")

    def view_contacts(self):
        if not self.contacts:
            print("No contacts found.")
        else:
            print("\nContacts List:")
            for name, details in self.contacts.items():
                print(f"Name: {name}, Phone: {details['Phone']}, Email: {details['Email']}")

    def delete_contact(self, name):
        if name in self.contacts:
            del self.contacts[name]
            print(f"Contact for {name} deleted successfully!")
        else:
            print(f"No contact found for {name}.")

    def run(self):
        while True:
            print("\n--- Contact Book ---")
            print("1. Add Contact")
            print("2. View Contacts")
            print("3. Delete Contact")
            print("4. Exit")

            choice = input("Enter your choice: ")

            if choice == '1':
                name = input("Enter contact name: ")
                phone = input("Enter phone number: ")
                email = input("Enter email address: ")
                self.add_contact(name, phone, email)

            elif choice == '2':
                self.view_contacts()

            elif choice == '3':
                name = input("Enter contact name to delete: ")
                self.delete_contact(name)

            elif choice == '4':
                print("Exiting Contact Book. Goodbye!")
                break

            else:
                print("Invalid choice. Please select a valid option.")

# Create a ContactBook instance and run the application
if __name__ == "__main__":
    contact_book = ContactBook()
    contact_book.run()

Program Explanation

The program is built around a class called ContactBook which contains the following methods:

  • __init__(): Initializes the contacts as an empty dictionary.
  • add_contact(name, phone, email): Adds a new contact to the contacts dictionary using the contact name as the key and a dictionary containing phone and email as the value.
  • view_contacts(): Displays all the contacts stored in the contact book.
  • delete_contact(name): Deletes the contact with the given name.
  • run(): The main method that provides a simple text-based interface for users to interact with the contact book. It allows them to add, view, and delete contacts or exit the program.

The program runs in a loop, continuously prompting the user for actions until they choose to exit. It handles basic validation and interaction, making it easy to store and manage contacts.

How to Run the Program

To run the program, follow these steps:

  1. Ensure you have Python installed on your computer. You can download it from here.
  2. Create a new Python file (e.g., contact_book.py) and copy the program code into that file.
  3. Open your terminal or command prompt, navigate to the folder where the Python file is saved, and run the program by typing: python contact_book.py
  4. Follow the on-screen prompts to add, view, or delete contacts.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

One thought on “Contact Book Application in Python”
  1. Greetings and Happy New Year,

    We are writing to express our interest in collaborating with you on an investment project that aligns with our mutual goals. We believe that a partnership between our organizations could yield significant benefits for both parties.

    We would be pleased to provide you with more information about the project upon receiving your response. Please do not hesitate to contact us if you require any additional information or would like to discuss this opportunity further.

    Best regards,

    RB

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)