+ 4

Dictionary functions

The result of this code is 8, why ? fib = {1: 1, 2: 1, 3: 2, 4: 3} print(fib.get(4, 0) + fib.get(7, 5))

28th Feb 2018, 3:44 PM
Stefano De Almeida
Stefano De Almeida - avatar
2 Answers
+ 6
general syntax : dict={key : value,.....} a=dict.get(key, default=None) b=dict.get(anotherkey, a value) #### if the key provided in get method is available in dict...its ok...if not that will add a new key with the passed value(default value is None) fib = {1: 1, 2: 1, 3: 2, 4: 3} print(fib.get(4, 0) + fib.get(7, 5)) >>>>>> fib.get(4, 0) =3 fib.get(7, 5)=5...as 7 isnt a key...it will add the key as 7 with value as 5... now fib ={ 1: 1, 2: 1, 3: 2, 4: 3, 7:5}
28th Feb 2018, 4:21 PM
sayan chandra
sayan chandra - avatar
+ 2
thank you so much sayan chandra!! :D
28th Feb 2018, 5:09 PM
Stefano De Almeida
Stefano De Almeida - avatar