DEV Community

Cristian Fernando
Cristian Fernando

Posted on

1

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

Explica este código Python

Dificultad: Fácil

test = {"a":1, "b":2, "c":3}
resultado = test.get("d", 0)
print(resultado)
Enter fullscreen mode Exit fullscreen mode
  • A. 1
  • B. 2
  • C. 3
  • D. 0

Respuesta:

👉 D. 0

Para acceder a los valores de un diccionario podemos usar la sintaxis de corchetes pero también existe el método get().

Este se caracteriza por que es posible asignarle un valor por defecto si es que la llave a la que queremos acceder en el diccionario no existe.

En nuestro ejemplo intentamos acceder al valor de la llave d pero como vemos esta llave no existe, entonces le asignamos el valor por defecto de nuestra preferencia, en este caso 0.

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 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