Skip to content
Programming Problems · Q5

Q.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
Tripura TbseTextbookSubjective· 5mImportance★★★★★est
90% · 27/30 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Store friends as name -> phone-number pairs; then a) loop and print, b) assign a new key, c) del a key, d) reassign an existing key, e) test with in, f) iterate over sorted(friends).

The idea. Key-value data with unique, meaningful keys (names) and one value each (phone number) is exactly what a dictionary models. Every operation the question asks for maps to a single dictionary idiom — no searching or index bookkeeping needed. (In a real program you would first fill the dictionary with an input() loop, friends[input("Name: ")] = input("Phone: "); here the data is pre-filled so each operation's effect is visible.)

Program:

friends = {'Tanvi': '9876512340', 'Rahul': '9123456780', 'Zoya': '9988776655'}

# a) display the name and phone number of all friends
print("a) All friends:")
for name in friends:
    print("  ", name, ":", friends[name])

# b) add a new key-value pair
friends['Kabir'] = '9012345678'
print("b) After adding Kabir :", friends)

# c) delete a particular friend
del friends['Rahul']
print("c) After deleting Rahul :", friends)

# d) modify the phone number of an existing friend
friends['Zoya'] = '9000011111'
print("d) After changing Zoya's number :", friends)

# e) check if a friend is present or not
name = 'Tanvi'
if name in friends:
    print("e)", name, "is present in the dictionary")
else:
    print("e)", name, "is not present in the dictionary")

# f) display the dictionary in sorted order of names
print("f) Dictionary in sorted order of names:")
for name in sorted(friends):
    print("  ", name, ":", friends[name])

Verified output:

a) All friends:
   Tanvi : 9876512340
   Rahul : 9123456780
   Zoya : 9988776655
b) After adding Kabir : {'Tanvi': '9876512340', 'Rahul': '9123456780', 'Zoya': '9988776655', 'Kabir': '9012345678'}
c) After deleting Rahul : {'Tanvi': '9876512340', 'Zoya': '9988776655', 'Kabir': '9012345678'}
d) After changing Zoya's number : {'Tanvi': '9876512340', 'Zoya': '9000011111', 'Kabir': '9012345678'}
e) Tanvi is present in the dictionary
f) Dictionary in sorted order of names:
   Kabir : 9012345678
   Tanvi : 9876512340
   Zoya : 9000011111

Operation-by-operation: …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.