how to check if a key is present in python dictionary 1

how to check if a key is present in python dictionary

dict = { "How":1,"you":2,"like":3,"this":4}
key = "this"
if key in dict.keys():
    print("present")
    print("value =",dict[key])
else:
    print("Not present")

Here is what the above code is Doing:
1. We create a dictionary with some key-value pairs.
2. We check if the key is present in the dictionary.
3. If the key is present, we print the value of the key.
4. If the key is not present, we print a message.

Similar Posts