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
0

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

Please explain this code

11th Jun 2018, 5:43 AM
Saurav nalla
1 Answer
0
This line: fib = {1: 1, 2: 1, 3: 2, 4: 3} initialize a dictionary called fid with four key-value pairs in it. fib.get method takes dictionary value by key (like fib.get(4,0) takes a value with key '4', if the key is absent in a dictionary, it will take a default value None or one that is set after the comma (0)). As soon as there is a value with key '4', the method returns '3' fib.get(7, 5) tries to get a value of key '7', which is absent in the dictionary, so it takes '5' as default (the value after the comma in fib.get(7, 5)).
11th Jun 2018, 6:34 AM
strawdog
strawdog - avatar