0

What does this code means?

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

28th Oct 2021, 4:52 AM
Neethu Nath
Neethu Nath - avatar
4 Réponses
+ 3
pairs is a dictionary, it associates every key to its value (key : value) pairs.get(<key>, <alternative>) gives you the value associated to <key>, but if the key is not found it gives you <alternative>
28th Oct 2021, 6:00 AM
Angelo
Angelo - avatar
+ 3
The number 42 *is* the alternative. So if the key does not exist the function returns the number 42.
28th Oct 2021, 6:16 AM
Simon Sauter
Simon Sauter - avatar
+ 1
28th Oct 2021, 6:25 AM
Neethu Nath
Neethu Nath - avatar
0
But, there is no alternative matching 42
28th Oct 2021, 6:04 AM
Neethu Nath
Neethu Nath - avatar