Ibuprofeno.py馃拪| #108: Explica este c贸digo Python

Cristian Fernando - May 16 - - Dev Community

Explica este c贸digo Python

Dificultad: Intermedio

person = {}
person['fname'] = 'Joe'
person['lname'] = 'Fonebone'
person['age'] = 51
person['spouse'] = 'Edna'
person['children'] = ['Ralph', 'Betty', 'Joey']
person['pets'] = {'dog': 'Fido', 'cat': 'Sox'}

print(person.get("pets").get("cat"))
Enter fullscreen mode Exit fullscreen mode

馃憠 A. Fido
馃憠 B. Sox
馃憠 C. SyntaxError
馃憠 D. TypeError


Respuesta:

馃憠 B. Sox

Los diccionarios pueden contener otros diccionarios en su interior. Esta sintaxis se llama diccionario anidados y para acceder a sus valores se debe concatenar m茅todos get() (como en este reto) o se puede usar tambi茅n la sintaxis de corchete:

print(person["pets"]["cat"]) # Sox
Enter fullscreen mode Exit fullscreen mode

Nota tambi茅n como creamos el diccionario, inicialmente person es un diccionario vac铆o y lo vamos llenando propiedad a propiedad asignado valores a diferentes claves.


. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .