Skip to the content

Project: contact book

Python · Unit 7: Dictionaries

You're going to build a contact book: a dictionary where each name is a key and their phone number is the value.

Three functions: add a contact, find their number and list the names that start with a letter.

book = {"Amy": "555-1234"}
book["Leo"] = "555-9876"
print(book["Leo"])
print(len(book))

Prints

555-9876
2

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Complete the code

    Complete add, which stores someone's phone number. If they were already there, it changes the number.

  2. 2. Write the code

    Write find(book, name), which returns that person's phone number, or "Not found" if they aren't in the book.

  3. 3. Write the code

    Write by_letter(book, letter), which returns a list of the names that start with that letter, in alphabetical order. If there are none, it returns an empty list.

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →