Dictionary Question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Dictionary Question

X = {1:2, 2:3, 3:4, 4:6} print(x.get(2, 0)%x.get(5, 4)) Solution: 3 So lost:(. It's a dictionary, but there is no key 5, so what're they looking for? Can't get any sums to give me %=3 either. Any ideas (any help appreciated:)).

30th Mar 2019, 7:32 AM
tristach605
tristach605 - avatar
2 Answers
+ 5
get returns the second argument, if it doesn't find the key. So if 2 wasn't in X, the first return value would be 0; since 5 isn't in the dictionary, the second return value will be 4. So it's 3%4 what's happening there.
30th Mar 2019, 7:38 AM
HonFu
HonFu - avatar
+ 2
The method get(key, default_value = None) returns a value for the given key. If key is not available then returns default_value. So get(5, 4) == 4 and 3%4 == 3. https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2451/
30th Mar 2019, 7:41 AM
portpass