Contact Book
Learn how to build a simple Contact Book using Python lists, dictionaries, functions, loops and search operations.
1. Project Introduction
A Contact Book is an application used to store and manage contact information.
A contact can contain information such as a name, phone number and email address.
2. Understanding Contact Data
A Python dictionary is useful for storing one contact.
contact = {
"name": "Rahul",
"phone": "9876543210",
"email": "rahul@example.com"
}
3. Creating the Contact List
Multiple contacts can be stored inside a list.
contacts = []
Every contact added to the application will be stored in this list.
4. Adding a Contact
We can create a dictionary and add it to the contacts list using append().
contact = {
"name": "Rahul",
"phone": "9876543210",
"email": "rahul@example.com"
}
contacts.append(contact)
5. Creating an Add Function
A function makes the program easier to organize.
def add_contact(name, phone, email):
contact = {
"name": name,
"phone": phone,
"email": email
}
contacts.append(contact)
6. Displaying All Contacts
A for loop can be used to display every contact.
for contact in contacts:
print("Name:", contact["name"])
print("Phone:", contact["phone"])
print("Email:", contact["email"])
print()
7. Searching for a Contact
We can compare the entered name with the name stored inside every dictionary.
search_name = input(
"Enter name to search: "
)
for contact in contacts:
if contact["name"].lower() == search_name.lower():
print(contact)
Using lower() makes the search case-insensitive.
8. Updating a Contact
Dictionary values can be changed using their keys.
contact["phone"] = "9999999999" contact["email"] = "new@example.com"
9. Deleting a Contact
The remove() method can be used when we already have the contact object.
contacts.remove(contact)
10. Checking Whether a Contact Exists
Before adding a contact, we can check whether the same phone number already exists.
exists = False
for contact in contacts:
if contact["phone"] == phone:
exists = True
break
11. Input Validation
User input should be checked before storing it.
if not name:
print("Name is required.")
elif not phone:
print("Phone number is required.")
else:
print("Contact is valid.")
You can also validate phone number length and email format in a more advanced version.
12. Menu-Based Contact Book
A while loop can keep the program running until the user chooses Exit.
while True:
print("1. Add Contact")
print("2. View Contacts")
print("3. Search Contact")
print("4. Update Contact")
print("5. Delete Contact")
print("6. Exit")
choice = input("Enter choice: ")
13. Using if / elif
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":
break
14. Creating Separate Functions
A clean Contact Book can be divided into multiple functions.
- add_contact()
- view_contacts()
- search_contact()
- update_contact()
- delete_contact()
This makes the program easier to understand, test and maintain.
15. Complete Project Flow
16. Concepts You Practiced
- Python Lists
- Python Dictionaries
- Dictionary Keys and Values
- append()
- remove()
- for Loops
- while Loops
- if / elif / else
- Functions
- String lower()
- Input Validation
- CRUD Operations
17. Run Complete Project 🚀
You have learned how the Contact Book works. Now open the complete interactive project.
Run Complete Project