python-tutorial-Python-courses-python-exercises-algorithm-test-if-given-key-is-present-in-dictionary

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

Leave a Reply