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