+ 1

please explain why result is 8

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)) #thank you

4th Feb 2020, 10:42 AM
Sergiu Simion Faragau
Sergiu Simion Faragau - avatar
3 Respostas
+ 7
get 4 = 3 and get 7 = not found then use 5. So it's 3 + 5 = 8
4th Feb 2020, 10:44 AM
r8w9
r8w9 - avatar
+ 1
The method fib.get() takes 2 parameters. The first will be the key to find and the second will be the default value if the key cannot be found. So basically: dictionary.get(key_to_find, default_value)
4th Feb 2020, 10:47 AM
KnuckleBars
KnuckleBars - avatar
+ 1
thank you both!
4th Feb 2020, 10:58 AM
Sergiu Simion Faragau
Sergiu Simion Faragau - avatar