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
Write the output of the following code:
d = { (1,2):1, (2,3):21 } print (d[1,2] )
Write the output of the following code:
d = {'a':1, 'b':2, 'c':3} print (d['a'])
Consider the following dictionary Prod_Price.
Prod_Price = {'LCD' : 25000, 'Laptop' : 35000, 'Home Theatre' : 80000, 'Microwave Oven' : 18000, 'Electric Iron' : 2800, 'Speaker' : 55000}
Find the output of the following statements:
(a) print(Prod_Price.get('Laptop'))
(b) print(Prod_Price.keys())
(c) print(Prod_Price.values())
(d) print(Prod_Price.items())
(e) print(len(Prod_Price))
(f) print('Speaker' in Prod_Price)
(g) print(Prod_Price.get('LCD'))
(h) del ProdPrice['Home Theatre'] print (ProdPrice)
Write a program that prompts the user for product names and prices, and store these key-value pairs in a dictionary where names are the keys and prices are the values. Also, write a code to search for a product in a dictionary and display its price. If the product is not there in the dictionary, then display the relevant message.