- 1
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)).with explanation
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)) with explanation
4 odpowiedzi
+ 5
Get method is use to return a value from a dictionary if it hold otherwise it will give you default value.
fib.get(4,0) #here 4 is key which you are finding in dictionary and 0 is default value means if 4 key is not in dictionary then it will give 0.but it will give 3 because 4 is in dictionary and 3 is the value of 4 key.
fib.get(7,5) #give you 5 because in fib dictionary there is no 7 key.
Now
3+5=8
+ 1
Syntax for get() is:
dict.get(key,default_value)
If it finds the key in the dict it uses it's value and if not it uses the default value
0
I think it's 8.
0
Ques : What is the result of this code?
program : fib = {1: 1, 2: 1, 3: 2, 4: 3}
print(fib.get(4, 0) + fib.get(7, 5))
output : 8