Concepts Covered:

- File handling (
open,read,write) - JSON module (
json.dump,json.load) - Lists and dictionaries
- Functions and loops
- Data persistence
๐ฏ Objective:
Create a simple, text-based contact book application that allows users to:
- Add new contacts
- View all saved contacts
- Search by name
- Edit existing contacts
- Delete contacts
- Automatically save and load data using a JSON file
๐ก Code (contact_book.py):
import json
import os
# File to store contacts
CONTACT_FILE = "contacts.json"
# Load contacts if the file exists
def load_contacts():
if os.path.exists(CONTACT_FILE):
with open(CONTACT_FILE, "r") as f:
return json.load(f)
return []
# Save contacts to the JSON file
def save_contacts(contacts):
with open(CONTACT_FILE, "w") as f:
json.dump(contacts, f, indent=4)
# Display all contacts
def view_contacts(contacts):
if not contacts:
print("\n๐ญ No contacts found.\n")
return
print("\n๐ Your Contacts:")
for i, contact in enumerate(contacts, start=1):
print(f"{i}. {contact['name']} - {contact['phone']} - {contact['email']}")
print()
# Add a new contact
def add_contact(contacts):
name = input("๐ค Enter name: ")
phone = input("๐ Enter phone number: ")
email = input("๐ง Enter email: ")
contacts.append({"name": name, "phone": phone, "email": email})
save_contacts(contacts)
print(f"โ
Contact '{name}' added successfully!\n")
# Search for a contact
def search_contact(contacts):
search_name = input("๐ Enter name to search: ").lower()
found = [c for c in contacts if search_name in c['name'].lower()]
if found:
print("\n๐ Search Results:")
for c in found:
print(f"๐ค {c['name']} | ๐ {c['phone']} | ๐ง {c['email']}")
else:
print("\nโ No contacts found with that name.\n")
# Edit an existing contact
def edit_contact(contacts):
name = input("โ๏ธ Enter the name of the contact to edit: ").lower()
for contact in contacts:
if contact['name'].lower() == name:
print(f"\nEditing contact: {contact['name']}")
new_name = input("๐ค New name (leave blank to keep current): ") or contact['name']
new_phone = input("๐ New phone (leave blank to keep current): ") or contact['phone']
new_email = input("๐ง New email (leave blank to keep current): ") or contact['email']
contact.update({"name": new_name, "phone": new_phone, "email": new_email})
save_contacts(contacts)
print(f"โ
Contact '{new_name}' updated successfully!\n")
return
print("โ ๏ธ Contact not found.\n")
# Delete a contact
def delete_contact(contacts):
name = input("๐๏ธ Enter name of contact to delete: ").lower()
updated_contacts = [c for c in contacts if c['name'].lower() != name]
if len(updated_contacts) < len(contacts):
save_contacts(updated_contacts)
print(f"โ
Contact '{name}' deleted successfully!\n")
else:
print("โ ๏ธ Contact not found.\n")
return updated_contacts
# Main program
def main():
contacts = load_contacts()
while True:
print("===== ๐ CONTACT BOOK =====")
print("1๏ธโฃ View Contacts")
print("2๏ธโฃ Add Contact")
print("3๏ธโฃ Search Contact")
print("4๏ธโฃ Edit Contact")
print("5๏ธโฃ Delete Contact")
print("6๏ธโฃ Quit")
choice = input("๐ Enter your choice: ")
if choice == "1":
view_contacts(contacts)
elif choice == "2":
add_contact(contacts)
elif choice == "3":
search_contact(contacts)
elif choice == "4":
edit_contact(contacts)
elif choice == "5":
contacts = delete_contact(contacts)
elif choice == "6":
print("๐ Exiting Contact Book. Goodbye!")
break
else:
print("โ ๏ธ Invalid choice. Try again!\n")
if __name__ == "__main__":
main()
๐ง Example Usage
python contact_book.py
Console Output Example:
===== ๐ CONTACT BOOK =====
1๏ธโฃ View Contacts
2๏ธโฃ Add Contact
3๏ธโฃ Search Contact
4๏ธโฃ Edit Contact
5๏ธโฃ Delete Contact
6๏ธโฃ Quit
๐ Enter your choice: 2
๐ค Enter name: Alice
๐ Enter phone number: 123456789
๐ง Enter email: alice@example.com
โ
Contact 'Alice' added successfully!
๐พ Notes
- All contacts are saved automatically in
contacts.json - You can open the file anytime to view or back up your data
- The edit feature allows keeping existing fields if you leave them blank