- 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

16th Nov 2018, 11:29 AM
Priyanshu Ghatiya
Priyanshu Ghatiya - avatar
4 Antworten
+ 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
16th Nov 2018, 4:58 PM
Maninder $ingh
Maninder $ingh - avatar
+ 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
16th Nov 2018, 11:32 AM
Ankit
Ankit - avatar
0
I think it's 8.
16th Nov 2018, 11:31 AM
Ankit
Ankit - avatar
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
26th May 2021, 12:25 PM
Madhavareddy
Madhavareddy - avatar