DEV Community

Cover image for Ibuprofeno.py💊| #108: Explica este código Python
Cristian Fernando
Cristian Fernando

Posted on • Edited on

1

Ibuprofeno.py💊| #108: Explica este código Python

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.


Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

Retry later