Python Projects - Contact Management System in Python

 Python Projects -  Contact Management System in Python

Python Projects -  Contact Management System in Python

This project is beginner-friendly and will help you understand basic Python concepts like lists, dictionaries, functions, loops, and file handling.

Python Projects for Beginners 

A Contact Management System is a very good practical tool for organizing and managing contact information. It makes easy the process of storing and retrieving details, making it uses both personal and professional use. The Python project we built uses a simple functional for example of such a system, perfect for beginners to understand the core concepts.

  1. Contact Management System using Python. This system will allow users to:
  2. Add new contacts.
  3. View all contacts.
  4. Search for a contact by name.
  5. Update a contact's details.
  6. Delete a contact.
  7. Exit
Step 1: Set Up the Project
Install Python: Confirm that Python is installed on your system. or You can download it from this website python.org.

Choose an IDE: 
Choose any IDE like PyCharm, VS Code, or even a simple text editor like Notepad.

Create a new Python file: Save it as contact_management.py.

Step 2: Define the Data Structure
Make a list of dictionaries to store contacts. Each contact will be represented as a dictionary with keys like name, phone, and email.

Python Code


# Initialize an empty list to store contacts
contacts = []
Step 3: Create the Main Menu
Now, we will create a menu to allow users to interact with the system.

Python Code


def display_menu():
    print("\nContact Management System")
    print("1. Add Contact")
    print("2. View Contacts")
    print("3. Search Contact")
    print("4. Update Contact")
    print("5. Delete Contact")
    print("6. Exit")
Step 4: Implement the Add Contact Function
This add contact function will allow users to add a new contact.

Python Code


def add_contact():
    print("\nAdd New Contact")
    name = input("Enter name: ")
    phone = input("Enter phone number: ")
    email = input("Enter email: ")

    # Create a dictionary for the new contact
    contact = {
        "name": name,
        "phone": phone,
        "email": email
    }

    # Add the contact to the list
    contacts.append(contact)
    print(f"Contact '{name}' added successfully!")
Step 5: Implement the View Contacts Function
View Contacts Function will display all the contacts stored in the list.

Python Code


def view_contacts():
    print("\nAll Contacts")
    if not contacts:
        print("No contacts found.")
    else:
        for index, contact in enumerate(contacts, start=1):
            print(f"{index}. Name: {contact['name']}, Phone: {contact['phone']}, Email: {contact['email']}")
Step 6: Implement the Search Contact Function
Search Contact Function will allow users to search for a contact by name.

Python Code


def search_contact():
    print("\nSearch Contact")
    search_name = input("Enter name to search: ")

    found_contacts = [contact for contact in contacts if search_name.lower() in contact['name'].lower()]

    if found_contacts:
        print("Matching Contacts:")
        for contact in found_contacts:
            print(f"Name: {contact['name']}, Phone: {contact['phone']}, Email: {contact['email']}")
    else:
        print("No matching contacts found.")
Step 7: Implement the Update Contact Function
Update Contact Function will allow users to update a contact's details.

Python Code


def update_contact():
    print("\nUpdate Contact")
    name_to_update = input("Enter the name of the contact to update: ")

    for contact in contacts:
        if contact['name'].lower() == name_to_update.lower():
            print(f"Current Details: Name: {contact['name']}, Phone: {contact['phone']}, Email: {contact['email']}")
            contact['name'] = input("Enter new name (leave blank to keep current): ") or contact['name']
            contact['phone'] = input("Enter new phone number (leave blank to keep current): ") or contact['phone']
            contact['email'] = input("Enter new email (leave blank to keep current): ") or contact['email']
            print("Contact updated successfully!")
            return
    print("Contact not found.")
Step 8: Implement the Delete Contact Function
Delete Contact Function will allow users to delete a contact.

Python Code


def delete_contact():
    print("\nDelete Contact")
    name_to_delete = input("Enter the name of the contact to delete: ")

    for contact in contacts:
        if contact['name'].lower() == name_to_delete.lower():
            contacts.remove(contact)
            print(f"Contact '{name_to_delete}' deleted successfully!")
            return
    print("Contact not found.")
Step 9: Implement the Main Program Loop
Main Program Loop will keep the program running until the user chooses to exit.

Python Code


def main():
    while True:
        display_menu()
        choice = input("Enter your choice (1-6): ")

        if choice == '1':
            add_contact()
        elif choice == '2':
            view_contacts()
        elif choice == '3':
            search_contact()
        elif choice == '4':
            update_contact()
        elif choice == '5':
            delete_contact()
        elif choice == '6':
            print("Exiting the Contact Management System. Goodbye!")
            break
        else:
            print("Invalid choice. Please try again.")
Step 10: Run the Program
Add the following code at the end of your file to start the program

Python Code


if __name__ == "__main__":
    main()
Use Full Code or Run full code in Pycharm or any IDE to get result.

Python Code


# Initialize an empty list to store contacts
contacts = []

def display_menu():
    print("\nContact Management System")
    print("1. Add Contact")
    print("2. View Contacts")
    print("3. Search Contact")
    print("4. Update Contact")
    print("5. Delete Contact")
    print("6. Exit")

def add_contact():
    print("\nAdd New Contact")
    name = input("Enter name: ")
    phone = input("Enter phone number: ")
    email = input("Enter email: ")

    contact = {
        "name": name,
        "phone": phone,
        "email": email
    }

    contacts.append(contact)
    print(f"Contact '{name}' added successfully!")

def view_contacts():
    print("\nAll Contacts")
    if not contacts:
        print("No contacts found.")
    else:
        for index, contact in enumerate(contacts, start=1):
            print(f"{index}. Name: {contact['name']}, Phone: {contact['phone']}, Email: {contact['email']}")

def search_contact():
    print("\nSearch Contact")
    search_name = input("Enter name to search: ")

    found_contacts = [contact for contact in contacts if search_name.lower() in contact['name'].lower()]

    if found_contacts:
        print("Matching Contacts:")
        for contact in found_contacts:
            print(f"Name: {contact['name']}, Phone: {contact['phone']}, Email: {contact['email']}")
    else:
        print("No matching contacts found.")

def update_contact():
    print("\nUpdate Contact")
    name_to_update = input("Enter the name of the contact to update: ")

    for contact in contacts:
        if contact['name'].lower() == name_to_update.lower():
            print(f"Current Details: Name: {contact['name']}, Phone: {contact['phone']}, Email: {contact['email']}")
            contact['name'] = input("Enter new name (leave blank to keep current): ") or contact['name']
            contact['phone'] = input("Enter new phone number (leave blank to keep current): ") or contact['phone']
            contact['email'] = input("Enter new email (leave blank to keep current): ") or contact['email']
            print("Contact updated successfully!")
            return
    print("Contact not found.")

def delete_contact():
    print("\nDelete Contact")
    name_to_delete = input("Enter the name of the contact to delete: ")

    for contact in contacts:
        if contact['name'].lower() == name_to_delete.lower():
            contacts.remove(contact)
            print(f"Contact '{name_to_delete}' deleted successfully!")
            return
    print("Contact not found.")

def main():
    while True:
        display_menu()
        choice = input("Enter your choice (1-6): ")

        if choice == '1':
            add_contact()
        elif choice == '2':
            view_contacts()
        elif choice == '3':
            search_contact()
        elif choice == '4':
            update_contact()
        elif choice == '5':
            delete_contact()
        elif choice == '6':
            print("Exiting the Contact Management System. Goodbye!")
            break
        else:
            print("Invalid choice. Please try again.")

if __name__ == "__main__":
    main()

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Ok, Go it!