Editor

Python Program to Make a Simple Phone Book

Beginner · Dictionaries

A phone book stores names with their phone numbers, which is a natural fit for a dictionary: the name is the key and the number is the value. This program adds, searches, updates and deletes contacts, with the menu choices written out in a list so it runs without any typing.

Program

phone_book.py
phone_book = {"Riya": "98765 43210", "Arjun": "91234 56789"}
# Each tuple is one menu choice: (action, name, number)
# or: action = input("add/search/update/delete: ") and name = input("Name: ")
actions = [
("add", "Meera", "99887 76655"),
("search", "Arjun", None),
("update", "Riya", "90000 11111"),
("delete", "Arjun", None),
("search", "Arjun", None),
]
for action, name, number in actions:
if action == "add":
phone_book[name] = number
print("Added", name)
elif action == "update" and name in phone_book:
phone_book[name] = number
print("Updated", name, "to", number)
elif action == "delete" and name in phone_book:
del phone_book[name]
print("Deleted", name)
elif action == "search" and name in phone_book:
print("Found", name, ":", phone_book[name])
else:
print(name, "is not in the phone book")
print("Phone book now:")
for name in sorted(phone_book):
print(" ", name, "-", phone_book[name])
Output
Added Meera
Found Arjun : 91234 56789
Updated Riya to 90000 11111
Deleted Arjun
Arjun is not in the phone book
Phone book now:
  Meera - 99887 76655
  Riya - 90000 11111

How it works

  1. The dictionary phone_book maps each name (the key) to a phone number (the value), stored as a string so that spaces and leading zeros are kept.
  2. The list actions stands in for a user picking options from a menu, and the comment shows the input() version.
  3. Adding and updating both use phone_book[name] = number, but the update branch first checks that the name exists.
  4. del removes a contact, and searching is a plain lookup once in has confirmed the name is there.
  5. A request for a name that is not in the book falls through to the last else, which is why the second search for Arjun says he is gone.

Other ways to write it

Search by part of a name

Search by part of a name
phone_book = {"Riya Sharma": "98765 43210", "Arjun Rao": "91234 56789", "Ria Thomas": "99887 76655"}
query = "ri" # or: query = input("Search: ")
matches = {name: number for name, number in phone_book.items() if query.lower() in name.lower()}
if matches:
for name, number in matches.items():
print(name, "-", number)
else:
print("No contact matches", query)
Output
Riya Sharma - 98765 43210
Ria Thomas - 99887 76655

Real phone books let you type part of a name. Lower-casing both sides makes the search ignore case, and the comprehension keeps every contact whose name contains the query.

Saving the phone book to a file

Saving the phone book to a file
import json
phone_book = {"Riya": "98765 43210", "Arjun": "91234 56789"}
# Save the contacts so they are still there the next time the program runs
with open("contacts.json", "w") as f:
json.dump(phone_book, f, indent=2)
with open("contacts.json") as f:
loaded = json.load(f)
loaded["Meera"] = "99887 76655"
print("Loaded from file:", loaded)
Output
Loaded from file: {'Riya': '98765 43210', 'Arjun': '91234 56789', 'Meera': '99887 76655'}

A dictionary disappears when the program ends. json.dump() writes it to a file and json.load() reads it back as a dictionary, so the contacts survive between runs.

Try it yourself

Add ("search", "Kabir", None) to the list of actions and check the message it prints.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises