May 2017
Intermediate to advanced
280 pages
6h 2m
English
The syntax of the get() method is as follows:
dict.get(key, default=None)
The get() method is used to get the value of a given key from the dictionary. If key is not found, then the default value or message will return. See the following example, where key is present:
>>> A1 = {'iron-man': 'Tony', 'CA': 'Steve', 'BW': 'Natasha', 'hulk': 'Bruce-Banner'} >>> A1.get('iron-man',"not found") 'Tony'
In the preceding example, since the key is found, the custom message, not found, does not get printed. Let's see another example:
>>> A1.get('panther',"not found") 'not found' >>> A1.get("Black") >>>
If the custom message is not set, then nothing will be returned. There is another method, setdefault(), which is very much similar to the ...
Read now
Unlock full access