pairs = {1: "apple", "orange": [2, 3, 4], True: False, 12: "True", } print(pairs.get("orange")) print(pairs.get(7, 42)) | Sololearn: Learn to code for FREE!

+2

pairs = {1: "apple", "orange": [2, 3, 4], True: False, 12: "True", } print(pairs.get("orange")) print(pairs.get(7, 42))

Can't understand print(pairs.get(7, 42))

5/25/2021 11:00:45 AM

prathiba singaravelu

3 Answers

New Answer

+5

get() take one required argument (the key), and one optional argument (the value to return if the key was not found). so get(7,42) will return 42 as there is no 7 key in your pairs dict ^^

0

0

prathiba singaravelu 1 - get(key) // it will return None if key is not exist 2 - get(key, defaultValue) //it will return defaultValue if key is not exist. So where you are sure that if key not exist then default value should be return then you can use 2nd function.