) Write a program to input your friends' names and their Phone Numbers and storethem in the dictionary as the key-value pair. Perform the following operations on thedictionary: (3)i) Display the name and phone number of all your friendsii) Add a new key-value pair in this dictionary and display the modifieddictionaryiii) Delete a particular friend from the dictionaryiv) Modify the phone number of an existing friendv) Check if a friend is present in the dictionary or notvi) Display the dictionary in sorted order of name
Question
) Write a program to input your friends' names and their Phone Numbers and storethem in the dictionary as the key-value pair. Perform the following operations on thedictionary: (3)i) Display the name and phone number of all your friendsii) Add a new key-value pair in this dictionary and display the modifieddictionaryiii) Delete a particular friend from the dictionaryiv) Modify the phone number of an existing friendv) Check if a friend is present in the dictionary or notvi) Display the dictionary in sorted order of name
Solution
Sure! Here is a step-by-step solution to the problem:
Step 1: Create an empty dictionary to store the friends' names and phone numbers. Step 2: Use a loop to input the names and phone numbers of your friends and add them as key-value pairs in the dictionary. Step 3: To display the name and phone number of all your friends, iterate over the dictionary and print each key-value pair. Step 4: To add a new friend's name and phone number, use the dictionary's "update" method to add a new key-value pair. Step 5: To delete a particular friend from the dictionary, use the "del" keyword followed by the key of the friend you want to delete. Step 6: To modify the phone number of an existing friend, use the dictionary's "update" method to update the value associated with the friend's key. Step 7: To check if a friend is present in the dictionary, use the "in" keyword followed by the key you want to check. Step 8: To display the dictionary in sorted order of name, use the "sorted" function with the dictionary's "items" method as the argument.
Here is an example implementation in Python:
# Step 1
friends_dict = {}
# Step 2
num_friends = int(input("Enter the number of friends: "))
for _ in range(num_friends):
name = input("Enter friend's name: ")
phone_number = input("Enter friend's phone number: ")
friends_dict[name] = phone_number
# Step 3
print("Name and phone number of all friends:")
for name, phone_number in friends_dict.items():
print(name, phone_number)
# Step 4
new_name = input("Enter new friend's name: ")
new_phone_number = input("Enter new friend's phone number: ")
friends_dict.update({new_name: new_phone_number})
# Step 5
delete_name = input("Enter the name of the friend you want to delete: ")
del friends_dict[delete_name]
# Step 6
modify_name = input("Enter the name of the friend whose phone number you want to modify: ")
new_phone_number = input("Enter the new phone number: ")
friends_dict.update({modify_name: new_phone_number})
# Step 7
check_name = input("Enter the name of the friend you want to check: ")
if check_name in friends_dict:
print(check_name, "is present in the dictionary.")
else:
print(check_name, "is not present in the dictionary.")
# Step 8
sorted_dict = dict(sorted(friends_dict.items(), key=lambda x: x[0]))
print("Dictionary in sorted order of name:")
for name, phone_number in sorted_dict.items():
print(name, phone_number)
This program allows you to input your friends' names and phone numbers, perform various operations on the dictionary, and display the results accordingly.
Similar Questions
Dictionary Creation: a. Create a dictionary named contacts with keys 'Alice' and 'Bob' mapped to their respective email addresses.Dictionary Operations: a. Retrieve the email address of 'Alice' from the dictionary contacts. b. Add a new key-value pair to contacts with 'Eric' mapped to '[email protected]'. c. Delete the entry for 'Alice' from the dictionary contacts.
Write a Python program to create a new dictionary by extracting the mentioned keys and create a new new dictionary and print it as the result.
Write a Python program to get a dictionary value from the user and count the number of keys that are having the same value and print the count.
Write a program that prints all the keys and values in a dictionary where the key is "dennis" and the value is "student."
Build a project called students’ management system which beable to do the tasks below:1. Add students’ information to the Dictionary2. Show all students in the Dictionary3. Search students by stu_id4. Update students’ information5. Delete any students from the DictionaryNote: students have ID, Name, Sex, DOB, Add, ResultDeadline: one week!
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.