In this example,Mapa_De_Digitos
is a dictionary type objetto, to see the type of an object use, type()
:
type(Mapa_De_Digitos)
Out[7]: dict
In case the get()
is one of the methods of the class that defines the dictionary object, to see the methods available in an object use dir(objeto)
, I won’t play below because the list is immense, but you can prove that get()
is in the list of methods like this:
'get' in dir(Mapa_De_Digitos)
Out[9]: True
Definition of dicionário
:
A Python dictionary is a data structure that, unlike sequences that are indexed by a crease of numbers, dictionaries are indexed by keys (Keys), that are imútavies types, strings and numbers can represent these keys
, according to the own documentation, the best way to think of a dictionary and as a set of key pairs:value (key:value). with the restriction that keys have to be unique (within a dictionary), an empty dictionary is defined by a key pair (braces)
The method get()
:
The method recovers the value assigned to the key Voce sends as argument to the method, but also Voce can do this with format dict[key]
, see below:
Mapa_De_Digitos.get("4")
Out[14]: 'Quatro'
Mapa_De_Digitos["4"]
Out[15]: 'Quatro'
The advantage of the method get()
is that it always returns something, if the key does not exist, the value None
is returned, while in parentheses format an error exception is raised when the key does not exist, see:
nine = Mapa_De_Digitos.get(9)
print(nine)
None
nine = Mapa_De_Digitos[9]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-20-bb87d7f1428a> in <module>()
----> 1 nine = Mapa_De_Digitos[9]
KeyError: 9
I think I just got it. Correct me if I’m wrong: # If "ch" from IMPUT equals the Mapa_de_digits List Key -> The "get" #...method shows the corresponding value (Value key) of the list item.
– Nameless Man
Your "for" loop will scan all digits of your input, one by one, into the variable "ch" and will test if it matches any key in your dictionary "Mapa_de_digitos", if it matches, it will print the value in full and if there is no key equal to "ch"will print an empty string.
– Thiago Krempser