![python-tutorial-Python-courses-python-exercises-algorithm-test-if-given-key-is-present-in-dictionary](https://1.bp.blogspot.com/-NA7a1e5zM9k/X3L62HQM02I/AAAAAAAAB4s/EC1bM40HfmExjfcc6TJ7I7PvyaVP1lsMQCLcBGAsYHQ/s16000/python-courses.png)
Exercise 117
Write a python algorithm as function that verify if a key is present in given dictionary or note
Solution
# define a dictionary d
d = {'a' : 13 , 'b' : 7 , 'c' : 3 , 'd' : 5 , 'e' : 23 , 'f' : 17 , 'g' : 8 , 'h' : 11}
# define a function that test if a given key is present in d or not
def is_key_in_d(key):
if key in d:
return True
else:
return False
# Testing algorithm
print(is_key_in_d('c')) # display : True
print(is_key_in_d('m')) # display : False
Younes Derfoufi
my-courses.net
my-courses.net