0

Dictionaries

pairs = {1:2,3:5,6:3} print(pairs.get(2,0)+pairs.get(6,4)) O/P: 3 Explain the output of this program.

23rd Jul 2017, 7:29 AM
nive dha
nive dha - avatar
2 Réponses
+ 5
get() takes two arguments, the first is the key to look for, the second one is the value to return if the key is not found. If the key is found then its value is returned, so in this case: 0 + 3 = 3
23rd Jul 2017, 7:47 AM
Maya
Maya - avatar
0
The get() method returns a value for a given key. If a key doesn't exist, the given value in the 2nd argument is used. It doesn't replace a value if the key exists key 2 doesn't exist, so the value 0 is used.. key 6 exists, so it's stored value is used, not the given one. edit: *frown* Maya, you cheat. I am typing on a phone! so much slower to reply. :( Or I am just slow. x.x
23rd Jul 2017, 7:48 AM
Sapphire