+ 1

I dont understand the output of this dictionary

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)) I don't get it, pls help.

15th Jan 2018, 9:25 AM
z1ckx33
z1ckx33 - avatar
2 Answers
+ 3
Got it myself, thanks just looked up the indexing. If 7 can't be found in the dictionary the get method assigns a new given value for 7 which is in this case 5. So the value for 4 is 3 and the new value for 7 is 5 --> 3+5=8
15th Jan 2018, 9:34 AM
z1ckx33
z1ckx33 - avatar
+ 2
get(key [, default]) returns value with the given key; if key doesn't exist it returns default (none by default) fib.get(4, 0) returns 3 fib.get(7, 5) returns 5
15th Jan 2018, 9:35 AM
BlazingMagpie
BlazingMagpie - avatar