DEV Community

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

Posted on

1

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

Explica este código Python

Dificultad: Básico

## Reto #62
my_dict = {"name": "John", "age": 30, "city": "New York"}

print("name" in my_dict)
print(len(my_dict))
Enter fullscreen mode Exit fullscreen mode

👉 A. True, 3
👉 B. True, 2
👉 C. True, TypeError
👉 D. False, 3


Respuesta en el primer comentario.

Top comments (1)

Collapse
 
duxtech profile image
Cristian Fernando • Edited

Respuesta:
👉 A. True, 3

Para saber si existe una determinada llave en el diccionario podemos usar el operador in que regresa True o False dependiendo que si existe o no dicha llave. Como name existe en my_dict regresamos True.

Usamos len para saber la longitud del diccionario, entendemos por longitud la cantidad de pares clave-valor que contiene el diccionario:

"name": "John" # 1
"age": 30 # 2
"city": "New York" # 3
Enter fullscreen mode Exit fullscreen mode

Hay tres pares clave-valor, entonces regresamos 3.

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

👋 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