+ 1

What's the output of this code..pls make me aware that what is p and 'p.get'?

p={1:5,2:5,3:6} print(p.get(8,4)+p.get(7,6))

13th May 2020, 5:59 PM
Bhanu Tripathi
Bhanu Tripathi - avatar
5 Respostas
+ 4
Output of this code should be 4 and 6 get is a dictionary method ,it looks for the key in the dict and returns it value ,and if there is no key with that name then the value you gave in get method is returned as it's value , so here p.get(8,4) looks for key 8 but since it is not there ,4 is returned as it's value
13th May 2020, 6:09 PM
Abhay
Abhay - avatar
+ 3
because 8 and 7 not found in dict it returns 4 and 6
13th May 2020, 6:11 PM
💡Arno Gevorgyan 🐍
💡Arno Gevorgyan 🐍 - avatar
13th May 2020, 6:04 PM
Kiran Deep Naidu
Kiran Deep Naidu - avatar
+ 2
Bhanu Tripathi "p" is a dictionary, whereas "p.get(x,y)" searches for the value of the key "x" in dictionary "p". If key "x" is not found it returns the "y".
13th May 2020, 6:07 PM
Kiran Deep Naidu
Kiran Deep Naidu - avatar
+ 1
I can't get that...so I asked!!
13th May 2020, 6:06 PM
Bhanu Tripathi
Bhanu Tripathi - avatar