+ 1

Dictionary Functions

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)) How this code give us the output is 8.?

21st Jun 2017, 1:08 PM
urvi
2 odpowiedzi
+ 3
the first get gets the value of key 4 ie 3, the second get looks for the key 7 value but since key 7 does not exist uses the default value you have set ie 5 5+3 = 8
21st Jun 2017, 1:19 PM
richard
+ 1
You used 2 arguments for Dictionary.get() in each case. The get() method defaults to argument 2 if argument one cannot be found: 4 gets 3 0 skipped 7 gets None 5 returned (default) 3+5=8
21st Jun 2017, 1:33 PM
Jamie
Jamie - avatar