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


Answer :-


dic = { }
while True :
    name = input( "Enter name of friend :-")
    phone = int( input("Enter phone number of friend :-" ))
    dic [name] = phone
    choise = input("Enter Q to quit otherwise N :-")
    if choise == "Q" or choise == "q" :
        break

#a
print()
print("a :-")
print(dic)

#b
print()
print("b :-")
name = input( "Enter name of friend :-")
phone = int(input("Enter phone number of friend :-" ))
dic [name] = phone
print(dic)

#c
print()
print("c :-")
name = input( "Enter name of that friend which you want to delete :-")
del dic[ name ]
print(dic)

#d
print()
print("d :-")
name = input( "Enter name of that friend which you want to change his phone number :-")
phone = int(input ("Enter phone number of friend :-" ))
del dic [ name]
dic[ name ] = phone
print(dic)

#e
print()
print("e :-")
name = input( "Enter name of that friend which you want to search :-")
if name in dic :
    print(" Found")
else :
    print( "Not Found ")

#f
print()
print("f :-")
newdic = {}
key = list( dic.keys())
key.sort()
for i in key :
    newdic [ i ] = dic [ i ]
print(newdic)


Output :-

=== RESTART: C:\Users\pw\Desktop\TEST.py ===


Enter name of friend :-path
Enter phone number of friend :-123456789
Enter Q to quit otherwise N :-n
Enter name of friend :-walla
Enter phone number of friend :-987654321
Enter Q to quit otherwise N :-n
Enter name of friend :-computer
Enter phone number of friend :-56789
Enter Q to quit otherwise N :-n
Enter name of friend :-portal
Enter phone number of friend :-123456
Enter Q to quit otherwise N :-n
Enter name of friend :-express
Enter phone number of friend :-13579
Enter Q to quit otherwise N :-q

a :-
{'path': 123456789, 'walla': 987654321, 'computer': 56789, 'portal': 123456, 'express': 13579}

b :-
Enter name of friend :-
python
Enter phone number of friend :-2468
{'path': 123456789, 'walla': 987654321, 'computer': 56789, 'portal': 123456, 'express': 13579, 'python': 2468}

c :-
Enter name of that friend which you want to delete :-
computer
{'path': 123456789, 'walla': 987654321, 'portal': 123456, 'express': 13579, 'python': 2468}

d :-
Enter name of that friend which you want to change his phone number :-
express
Enter phone number of friend :-23579
{'path': 123456789, 'walla': 987654321, 'portal': 123456, 'python': 2468, 'express': 23579}

e :-
Enter name of that friend which you want to search :-computer
Not Found

f :-
{'express': 23579, 'path': 123456789, 'portal': 123456, 'python': 2468, 'walla': 987654321}

>>>


Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post