DEV Community

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

Posted on • Edited on

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

Explica este código Python

Dificultad: Intermedio

## Reto #59
my_dict = dict(["a1", "b2", "c3"])

print(my_dict.get("d"))
print(my_dict["d"])
Enter fullscreen mode Exit fullscreen mode

👉 A. 0, 0
👉 B. KeyError, KeyError
👉 C. None, KeyError
👉 D. None, None


Respuesta en el primer comentario.

Top comments (1)

Collapse
 
duxtech profile image
Cristian Fernando • Edited

** Respuesta: **
👉 C. None, KeyError

En Python podemos acceder a los valores de un diccionario de 2 maneras diferentes:

  • Usando el método get(): regresa None en caso de que el par clave-valor no exista en el diccionario.
  • Usando la notación de corchetes: regresa el error KeyError y detiene la ejecución del programa.

Ambas maneras sirven para lo mismo pero la diferencia radica en cuando no encontramos la propiedad en el diccionario. Esta característica es muy peculiar de Python y esta bueno saberlo.

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

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay