+ 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))
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 ^^
0
Thank you
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.