+ 1
Explain this output
fib = {1:1, 2:1, 3:2, 4:3} print(fib.get(4,0)+fib.get(7,5)) result: 8 Now, I thought that the ,get() method returns the value of the key that you pass to it. But I know that i'm missing something because, if this is the case, fib.get(4,0) would be 3 and None, and fib.get(7,5) would be None and None, unless passing two arguments means something else. What am i missing? and why was it 8 instead?
5 Answers
+ 9
get() takes two arguments : the key and the default value. By default, the default value is None, so you can use it with only one argument.
What you are doing here is the sum of fib.get(4) and 5 as fib.get(7) returns None, so the get() function returns the second value you gave (in this case, 5).
+ 4
fib = {1:1, 2:1, 3:2, 4:3}
print(fib.get(4,0)+fib.get(7,5))
here when the print statement execute first it will find key 4 "(fib.get(4))" if it is found then it will return its value from fib{} in our case it is 3, if key 4 was not there in the fib than it will return whatever be the second argument you put in get() in our case "fib.get(4,0)" it will return 0 if the key 4 was not found ... now as it will return 3 so after this our print statement will look like " print(3 +fib.get(7,5)) " here there is no key like 7 in our fib so it will return the second argument you passed in get() ...in our case it is 5 ...........after this our print statement will look like " print(3 + 5) " and that is 8
0
easy to llearn for this web site.
0
#this code will definitely help you to clear this doubt.
pairs = {1: "apple",
"orange": [2, 3, 4],
True: False,
None: "True",
}
print(pairs.get("orange"))
print(pairs.get(7))
print(pairs.get(12345, "not in dictionary")) #look at the print statement here
the "get" statement is taking two
of
the arguments it is checking for
first in dictionary and then its
returning by default values which
you have specified in print
statement.
output:
>>>
[2, 3, 4]
None # here none is the by default value in python none means
no value.
not in dictionary #but here you have specified it the by default value.
>>>
#i hope so this will help you Thank You
0
after understanding this code it is obvious to say that print (2 + 3) will give 5.