Write a Python program to check whether a given key already exists in a dictionary


The program creates a dictionary d with two key-value pairs "Name" and "Age". The variable i is assigned the value "City". An if statement checks if the value of i is present as a key in the dictionary d using the in operator. If i is found in d, the program prints "Key is Available in the Dictionary". If i is not found in d, the program prints "Key is not Available in the Dictionary". In this case, since "City" is not a key in d, the program will print "Key is not Available in the Dictionary". In summary, the program demonstrates how to check if a key is present in a dictionary in Python using the in operator.

Source Code

d = {"Name":"Ram" , "Age":23}
"""
if "Name" in d:
  print('Key is Available in the Dictionary')
else:
  print('Key is not Available in the Dictionary')
"""
i="City"
if i in d:
  print('Key is Available in the Dictionary')
else:
  print('Key is not Available in the Dictionary')

Output

Key is not Available in the Dictionary