+ 1
Can someone explain me last question (dictionary function)
6 Answers
+ 2
Do you mind posting the question here? :)
+ 2
I already explained how dict.get works here:
https://www.sololearn.com/Discuss/357763
If you read that answer, you should be able to answer this question on your own. For more questions on that, please use that thread.
Answer should be 8 by the way.
+ 2
8 = 3 (See in dictionary key 4) + 5 (see the default value)
+ 1
What is the result of this code?
fib = {1: 1, 2: 1, 3: 2, 4: 3}
print(fib.get(4, 0) + fib.get(7, 5))
----------------------------------------
Answer:
fib.get(4,0) is looking for key 4 in fib, and 4 has a value 3 so: fib.get(4,0) = 3
Next, fib.get(7, 5) is looking for key 7, but there is no such a key. There are only keys from 1 to 4! So because there is no 7 key, it assigns a value 5.
And 3 + 5 = 8
print(fib.get(4, 0) + fib.get(7, 5))
3 + 5
0
a ={1:1, 2:1, 3:2, 4:3}
print(a.get(4,0)+a.get(7,5))
0
what is default value?