how they get not in dictionary | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how they get not in dictionary

pairs = {1: "apple", "orange": [2, 3, 4], True: False, None: "True", } print(pairs.get("orange")) print(pairs.get(7)) print(pairs.get(12345, "not in dictionary")) results: >>> [2, 3, 4] None not in dictionary

3rd Apr 2019, 8:07 PM
Smail Benloukil
Smail Benloukil - avatar
3 Answers
+ 10
your keys are : 1, "orange" , True, None as Tibor Santa explained, if the key is not in dictionary, the second parameter will be returned, or None if there's no second parameter given. so you have three lines here. 1 : "orange" is a key in your dict, its value is returnes : [2,3,4] 2 : 7 is not a key, default "None" is returned 3 : 12345 is not a key, second parameter "not in dictionary" is returned. To better understand, you may try print(pairs.keys()) print(pairs.values())
3rd Apr 2019, 8:42 PM
Cépagrave
Cépagrave - avatar
+ 3
The second parameter of get() is the default value that is returned when the key does not exist in the dict.
3rd Apr 2019, 8:30 PM
Tibor Santa
Tibor Santa - avatar
0
Thanks
4th Apr 2019, 5:24 PM
Smail Benloukil
Smail Benloukil - avatar