Friday, 10 December 2021

Pythone-Slip 3 A-Write a Python program to check if a given key already exists in a dictionary. If key exists replace with another key/value pair.

Write a Python program to check if a given key already exists in a dictionary. If key exists replace with another key/value pair.

 

d={'a':1,'B':2,'C':3}

 key=input("Enter key to check:")

 if key in d.keys():

       print("Key is present and value of the key is:")

       print(d[key])

 else:

       print("Key isn't present!")

 new_key = "A"

 old_key = "a"

 d[new_key] = d.pop(old_key)

 print(d)