Welcome! Today, we will explore creating a simple address book application using Python dictionaries. This task will help you understand manipulating dictionaries in Python, focusing on adding, retrieving, and deleting entries. By the end of this lesson, you'll have a solid grasp of these fundamental operations.
In this task, we will implement three methods to manage our address book:
add_contact(self, name: str, phone_number: str) -> bool
: Adds a new contact. Returns False
if the contact already exists, otherwise adds the contact and returns True
. In this task, let's assume phone numbers do not change, so it's not allowed to overwrite the existing contact's number.get_contact(self, name: str) -> str | None
: Retrieves the phone number for a given name
. Returns None
if the contact does not exist.delete_contact(self, name: str) -> bool
: Deletes a contact with the given name
. Returns True
if the contact exists and is deleted, False
otherwise.Let's break down each method in detail in the next sections.
This method adds a new contact to the address book with the given name
and phone_number
. If the contact already exists, it returns False
. Otherwise, it adds the contact and returns True
.
Question: Why do you think we need to check if the contact already exists?
Answer: To avoid duplicating existing entries. If a contact with the same name already exists, we shouldn't allow overwriting its phone number in this method, as it's only for creation.
Here is the method implementation:
Python1class AddressBook: 2 def __init__(self): 3 self.contacts = {} 4 5 def add_contact(self, name: str, phone_number: str) -> bool: 6 if name in self.contacts: 7 return False 8 self.contacts[name] = phone_number 9 return True 10 11# Example usage: 12address_book = AddressBook() 13print(address_book.add_contact("Alice", "123-456-7890")) # True 14print(address_book.add_contact("Alice", "098-765-4321")) # False 15print(address_book.contacts) # {'Alice': '123-456-7890'}
In this method:
if name in self.contacts
.False
.True
.This method retrieves the phone number associated with a given name
. If the contact does not exist, it returns None
.
Question: What do we gain by returning None
when a contact doesn't exist?
Answer: It provides a clear indicator that the contact is not in the address book, allowing us to handle such cases gracefully.
Here is the method implementation:
Python1class AddressBook: 2 def __init__(self): 3 self.contacts = {} 4 5 def add_contact(self, name: str, phone_number: str) -> bool: 6 if name in self.contacts: 7 return False 8 self.contacts[name] = phone_number 9 return True 10 11 def get_contact(self, name: str) -> str | None: 12 return self.contacts.get(name) 13 14# Example usage: 15address_book = AddressBook() 16address_book.add_contact("Alice", "123-456-7890") 17print(address_book.get_contact("Alice")) # '123-456-7890' 18print(address_book.get_contact("Bob")) # None
In this method:
get
method of the dictionary to retrieve the phone number.get
will return None
.This method deletes a contact with the given name
. If the contact exists and is deleted, it returns True
. If the contact does not exist, it returns False
.
Question: What could be a real-world consequence of not checking if the contact exists before deletion?
Answer: Attempting to delete a non-existent contact could result in errors or unintended behavior.
Here is the method implementation:
Python1class AddressBook: 2 def __init__(self): 3 self.contacts = {} 4 5 def add_contact(self, name: str, phone_number: str) -> bool: 6 if name in self.contacts: 7 return False 8 self.contacts[name] = phone_number 9 return True 10 11 def get_contact(self, name: str) -> str | None: 12 return self.contacts.get(name) 13 14 def delete_contact(self, name: str) -> bool: 15 if name in self.contacts: 16 del self.contacts[name] 17 return True 18 return False 19 20# Example usage: 21address_book = AddressBook() 22address_book.add_contact("Alice", "123-456-7890") 23print(address_book.delete_contact("Alice")) # True 24print(address_book.delete_contact("Bob")) # False 25print(address_book.contacts) # {}
In this method:
if name in self.contacts
.del self.contacts[name]
and return True
.False
.That's it! We now have the AddressBook
class fully implemented, and all three methods are working and well functional!
Dictionaries are particularly efficient for managing an address book due to several reasons:
These characteristics make Python dictionaries an ideal choice for implementing an address book.
In this lesson, we created a simple address book application using Python dictionaries. We implemented methods to add, retrieve, and delete contacts. Each step was built upon the previous one, enhancing our understanding of manipulating dictionaries in Python. Happy coding!