+ 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))

25th May 2021, 11:00 AM
prathiba singaravelu
prathiba singaravelu - avatar
3 Respostas
+ 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 ^^
25th May 2021, 11:03 AM
visph
visph - avatar
0
Thank you
25th May 2021, 11:06 AM
prathiba singaravelu
prathiba singaravelu - avatar
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.
25th May 2021, 1:50 PM
A͢J
A͢J - avatar