KnowledgeBoat Logo
|
LoginJOIN NOW

Informatics Practices

Write a program to input your friends' names and their phone numbers and store them in a dictionary as the key-value pair. Perform the following operations on the dictionary:

(a) Display the Name and Phone numbers 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.

Python Dictionaries

11 Likes

Answer

phonebook = {}

num_friends = int(input("Enter how many friends: "))

for i in range(num_friends):
    name = input("Enter name of friend: ")
    phone = input("Enter phone number: ")
    phonebook[name] = phone

# (a) Display the Name and Phone numbers of all your friends.
print("Friends and their phone numbers:")
for name, number in phonebook.items():
    print(name + ": " + number)

# (b) Add a new key-value pair in this dictionary and display the modified dictionary.
new_name = input("\nEnter name of new friend: ")
new_number = input("Enter phone number of new friend: ")
phonebook[new_name] = new_number
print(phonebook)

# (c) Delete a particular friend from the dictionary.
del_name = input("\nEnter name of friend to delete: ")
if del_name in phonebook:
    del phonebook[del_name]
    print(del_name + " has been deleted.")
else:
    print(del_name + " not found.")
print(phonebook)

# (d) Modify the phone number of an existing friend.
mod_name = input("\nEnter name of friend to modify number: ")
if mod_name in phonebook:
    mod_number = input("Enter new phone number: ")
    phonebook[mod_name] = mod_number
    print(mod_name + "'s phone number has been modified.")
else:
    print(mod_name + " not found.")
print(phonebook)

# (e) Check if a friend is present in the dictionary or not.
check_name = input("\nEnter name of friend to check: ")
if check_name in phonebook:
    print(check_name + " is present in the dictionary.")
else:
    print(check_name + " is not present in the dictionary.")

# (f) Display the dictionary in sorted order of names.
sorted_keys = sorted(phonebook)
print("\nDictionary in sorted order of names:")
print("{", end = " ")
for key in sorted_keys:
    print(key, ":", phonebook[key], end = " ")
print("}")

Output

Enter how many friends: 3
Enter name of friend: Mayuri
Enter phone number: 7689874888
Enter name of friend: Ankit
Enter phone number: 6748584757
Enter name of friend: Ashish
Enter phone number: 9088378388
Friends and their phone numbers:
Mayuri: 7689874888
Ankit: 6748584757
Ashish: 9088378388

Enter name of new friend: Sahini
Enter phone number of new friend: 8978909877
{'Mayuri': '7689874888', 'Ankit': '6748584757', 'Ashish': '9088378388', 'Sahini': '8978909877'}

Enter name of friend to delete: Mayuri
Mayuri has been deleted.
{'Ankit': '6748584757', 'Ashish': '9088378388', 'Sahini': '8978909877'}

Enter name of friend to modify number: Ankit
Enter new phone number: 8989587587
Ankit's phone number has been modified.
{'Ankit': '8989587587', 'Ashish': '9088378388', 'Sahini': '8978909877'}

Enter name of friend to check: Ashish
Ashish is present in the dictionary.

Dictionary in sorted order of names:
{ Ankit : 8989587587 Ashish : 9088378388 Sahini : 8978909877 }

Answered By

2 Likes


Related Questions