+ 3

Doubt related to dictionary

if x={1:2,2:3,3:4,4:6} .then what will be the value of print(x.get(5,4)) and how?

3rd Mar 2017, 5:26 AM
Anish Kumar Arya
Anish Kumar Arya - avatar
3 odpowiedzi
+ 5
https://www.tutorialspoint.com/JUMP_LINK__&&__python__&&__JUMP_LINK/dictionary_get.htm x.get(a,b) will return the value associated with key 'a' of the dict variable 'x'. If key 'a' doesn't exist in the dictionary, then it will return the default value 'b'.
3rd Mar 2017, 6:07 AM
Álvaro
+ 4
thanks. understood it very well.
3rd Mar 2017, 7:27 AM
Anish Kumar Arya
Anish Kumar Arya - avatar
+ 3
x.get(5, 4) The first argument passed (5) to the get method is the key to retrieve. The second number (4) is the default value if that key doesn't exist. So, in this case 4 will be returned as there is no key 5 in the dictionary.
3rd Mar 2017, 6:08 AM
ChaoticDawg
ChaoticDawg - avatar