Checking if a key exists in a dictionary python

By John

This is a Python Program to check if a given key exists in a dictionary or not.

Problem Description
The program takes a dictionary and checks if a given key exists in a dictionary or not.

Problem Solution
1. Declare and initialize a dictionary to have some key-value pairs.
2. Take a key from the user and store it in a variable.
3. Using an if statement and the in operator, check if the key is present in the dictionary using the dictionary.keys() method.
4. If it is present, print the value of the key.
5. If it isn’t present, display that the key isn’t present in the dictionary.
6. Exit.

Program/Source Code
Here is source code of the Python Program to check if a given key exists in a dictionary or not. The program output is also shown below.

d={‘A’:1,’B’:2,’C’:3}
key=raw_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!”)

Runtime Test Cases

Case 1:
Enter key to check:A
Key is present and value of the key is:
1

Case 2:
Enter key to check:F
Key isn’t present!