Contact Book Project in Python for Beginners
Creating a simple contact book project in Python is a great way for beginners to practice their programming skills. Below is a basic implementation of a contact book using dictionaries in Python:
def add_contact(contacts, name, number):
contacts[name] = number
print(f"Contact '{name}' added successfully!")
def delete_contact(contacts, name):
if name in contacts:
del contacts[name]
print(f"Contact '{name}' deleted successfully!")
else:
print(f"Contact '{name}' not found!")
def search_contact(contacts, name):
if name in contacts:
print(f"Name: {name}, Number: {contacts[name]}")
else:
print(f"Contact '{name}' not found!")
def display_contacts(contacts):
if contacts:
print("Contacts:")
for name, number in contacts.items():
print(f"Name: {name}, Number: {number}")
else:
print("No contacts to display.")
def main():
contacts = {}
while True:
print("\n--- Contact Book Menu ---")
print("1. Add Contact")
print("2. Delete Contact")
print("3. Search Contact")
print("4. Display Contacts")
print("5. Exit")
choice = input("Enter your choice (1-5): ")
if choice == '1':
name = input("Enter contact name: ")
number = input("Enter contact number: ")
add_contact(contacts, name, number)
elif choice == '2':
name = input("Enter contact name to delete: ")
delete_contact(contacts, name)
elif choice == '3':
name = input("Enter contact name to search: ")
search_contact(contacts, name)
elif choice == '4':
display_contacts(contacts)
elif choice == '5':
print("Exiting...")
break
else:
print("Invalid choice. Please enter a number from 1 to 5.")
if __name__ == "__main__":
main()
This code provides a simple command-line interface for adding, deleting, searching, and displaying contacts. When you run the program, it will present a menu of options, and you can choose what action to perform. The contacts are stored in a dictionary where the key is the contact name and the value is the contact number.
Watch Now:-https://www.youtube.com/watch?v=4sEfkbqDWJM&t=68s