Write a program to input your friends’ names and their Phone Numbers and store them in the
dictionary as the key-value pair. Perform the following operations on the dictionary:
a) Display the name and phone number of all your friends
b) Add a new key-value pair in this dictionary and display the modified dictionary
c) Delete a particular friend from the dictionary
d) Modify the phone number of an existing friend
e) Check if a friend is present in the dictionary or not
f) Display the dictionary in sorted order of names
Answers
Answer:
Explanation:
ph_dict = dict()
n = int(input("Enter the number of entries: "))
print()
for i in range(n):
x = input("Enter the name: ")
y = input("Enter the number: ")
print()
ph_dict[x] = y
print()
print("Your given dictionary: ") #(a)
print("Name", "\t\t", "Number")
for i in ph_dict:
print(i, "\t\t", ph_dict[i])
print()
nph = input("Enter the new entry's name: ") #(b)
nphn = input("Enter the number: ")
ph_dict[nph] = nphn
print()
print("Your updated dictionary: ")
print("Name", "\t\t", "Number")
for i in ph_dict:
print(i, "\t\t", ph_dict[i])
print()
dph = input("Enter the name of the person whose details you'd like to remove: ") #(c)
if dph in ph_dict:
del ph_dict[dph]
else:
print()
print("No such entry.")
print()
print("Your updated dictionary: ")
print("Name", "\t\t", "Number")
for i in ph_dict:
print(i, "\t\t", ph_dict[i])
print()
mph = input("Enter the name of the person whose details you'd like to modify: ") #(d)
if mph in ph_dict:
mphn = input("Enter the new number: ")
ph_dict[mph] = mphn
else:
print()
print("No such entry.")
print()
print("Your updated dictionary: ")
print("Name", "\t\t", "Number")
for i in ph_dict:
print(i, "\t\t", ph_dict[i])
print()
eph = input("Enter the entry whom you'd like to check is existing in the dictionary or not: ") #(e)
print()
if eph in ph_dict:
print("Entry exists in the dictionary.")
else:
print("No such entry.")
print()
print("Your dictionary in the sorted order of names: ") #(f)
print("Name", "\t\t", "Number")
for i in sorted(ph_dict):
print(i, "\t\t", ph_dict[i])
Answer:
ph_dict = dict()
n = int(input("Enter the number of entries: "))
print()
for i in range(n):
x = input("Enter the name: ")
y = input("Enter the number: ")
print()
ph_dict[x] = y
print()
print("Your given dictionary: ") #(a)
print("Name", "\t\t", "Number")
for i in ph_dict:
print(i, "\t\t", ph_dict[i])
print()
nph = input("Enter the new entry's name: ") #(b)
nphn = input("Enter the number: ")
ph_dict[nph] = nphn
print()
print("Your updated dictionary: ")
print("Name", "\t\t", "Number")
for i in ph_dict:
print(i, "\t\t", ph_dict[i])
print()
dph = input("Enter the name of the person whose details you'd like to remove: ") #(c)
if dph in ph_dict:
del ph_dict[dph]
else:
print()
print("No such entry.")
print()
print("Your updated dictionary: ")
print("Name", "\t\t", "Number")
for i in ph_dict:
print(i, "\t\t", ph_dict[i])
print()
mph = input("Enter the name of the person whose details you'd like to modify: ") #(d)
if mph in ph_dict:
mphn = input("Enter the new number: ")
ph_dict[mph] = mphn
else:
print()
print("No such entry.")
print()
print("Your updated dictionary: ")
print("Name", "\t\t", "Number")
for i in ph_dict:
print(i, "\t\t", ph_dict[i])
print()
eph = input("Enter the entry whom you'd like to check is existing in the dictionary or not: ") #(e)
print()
if eph in ph_dict:
print("Entry exists in the dictionary.")
else:
print("No such entry.")
print()
print("Your dictionary in the sorted order of names: ") #(f)
print("Name", "\t\t", "Number")
for i in sorted(ph_dict):
print(i, "\t\t", ph_dict[i])
Explanation: