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

Cristian Fernando - May 1 - - Dev Community

Explica este código Python

Dificultad: Fácil

bebidas = ["Agua", "Coca-Cola"]
bebidas[2] = "Pepsi"
print(bebidas)
Enter fullscreen mode Exit fullscreen mode

👉 A. ["Agua", "Coca-Cola", "Pepsi"]
👉 B. ["Agua", "Pepsi"]
👉 C. TypeError
👉 D. IndexError


Respuesta:

👉 D. IndexError

En Python no solo es posible acceder a los items de un arreglo por su índice, también podemos modificar elementos accediendo a un índice en particular.

El detalle de nuestro ejemplo es que queremos modificar una posición de nuestro arreglo que no existe, ello deriva a un error de tipo IndexError.

Una solución podría ser sustituir un item que si existe, por ejemplo:

bebidas = ["Agua", "Coca-Cola"]
bebidas[1] = "Pepsi"
print(bebidas) # ['Agua', 'Pepsi']
Enter fullscreen mode Exit fullscreen mode

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player