fib = {1: 1, 2: 1, 3: 2, 4: 3} print(fib.get(4, 0) + fib.get(7, 5)) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

fib = {1: 1, 2: 1, 3: 2, 4: 3} print(fib.get(4, 0) + fib.get(7, 5))

What is the output? it is shown the output as 8. How does it come?

9th May 2017, 4:28 AM
AJITH KUMAR
AJITH KUMAR - avatar
2 Answers
+ 9
get(4,0) tries to fetch the value associated with key 4, and if that key is not found, returns 0. In this case, there's a value for key 4, which is 3, thus 3 is returned. Similarly, get(7,5) looks for key 7 into the dictionary. But there's no such key, and then the default value 5 is returned. Now do the math: 3 + 5.
9th May 2017, 5:38 AM
Álvaro
+ 2
The get not explained very well... Syntax Following is the syntax for get() method − dict.get(key, default=None) Parameters key -- This is the Key to be searched in the dictionary. default -- This is the Value to be returned in case key does not exist. Return Value This method return a value for the given key. If key is not available, then returns default value None.
16th May 2017, 6:51 PM
Spiros Baden
Spiros Baden - avatar