DEV Community

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

Posted on

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

Explica este código Python

Dificultad: Fácil

k = [2, 1, 0, 3, 0, 2, 1]
print(k.count(k.index(0)))
Enter fullscreen mode Exit fullscreen mode
  • A. 2
  • B. 1
  • C. 0
  • D. 3

Respuesta:

👉 A. 2

Al usar el método index con listas, le pasamos un posible elemento que puede o no estar en la lista y nos regresa el índice de la primera coincidencia que se encuentre.

En nuestro caso nuestra lista tiene dos items 0 en diferentes posiciones, entonces regresamos el índice el primero que halle que seria 2.

Posteriormente usamos el método count para contar cuantas veces ese 2 obtenido se repite en la lista en general, y como podemos ver existen dos items 2.

Finalmente imprimimos 2 por consola.

No debemos confundir que el 2 que nos regresa index es justamente un índice, mientras que el el 2 que regresa count es un item de la lista.

Billboard image

Monitoring as code

With Checkly, you can use Playwright tests and Javascript to monitor end-to-end scenarios in your NextJS, Astro, Remix, or other application.

Get started now!

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

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