Why it Outputs 8 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Why it Outputs 8

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

2nd Mar 2022, 8:46 AM
David Mphande
David Mphande - avatar
3 Answers
+ 5
DAVID MPHANDE fib.get(4,0) is looking to call fib[4] which already has the value 3 assigned to it, so ignores the attached value in your call. fib.get(7,5) is looking to call a key: value that does not yet exist in your fib dictionary, so it assigns that key: value to the dict 3 + 5 = 8 #Why it Outputs 8 fib = {1: 1, 2: 1, 3: 2, 4: 3} print(fib.get(4, 0) + fib.get(7, 5)) print(fib[4]) print(fib[7])
2nd Mar 2022, 9:01 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
The output is 8 because from the in print it's written that - fib.get(4,0) # This gives you the value of 4 in the dictionary which is 3 and did not give 0 because 4 is there in the dictionary. fib.get(7,5) # This gives you the value of 7 but the key 7 is not there in the dictionary so it gives the value 5 which has been given in the second parameter. So the output is 3+5 = 8.
2nd Mar 2022, 9:02 AM
Shaurya Kushwaha
+ 2
Ooh 😮 i now get it, Thanks 👍
2nd Mar 2022, 9:06 AM
David Mphande
David Mphande - avatar