DEV Community

Cristian Fernando
Cristian Fernando

Posted on

2

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

Explica este código Python

Dificultad: Fácil

my_list = [1,2,3,4,5]
res = my_list[-3:-1]
print(res)
Enter fullscreen mode Exit fullscreen mode
  • A. [3, 4]
  • B. [2, 3]
  • C. [3, 4, 5]
  • D. [1, 2, 3, 4]

Respuesta:

👉 A. [3, 4]

Podemos hacer slicing de listas en Python usando índices negativos, los índices negativos empiezan a contar sobre la lista derecha a izquierda empezando por -1.

Entonces al decir my_list[-3:-1] tendremos [3, 4].

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