DEV Community

Cristian Fernando
Cristian Fernando

Posted on

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

Explica este código Python

Dificultad: Intermedio

x = [1, 2, 3]
y = x
print(x is y)
Enter fullscreen mode Exit fullscreen mode
  • A. True
  • B. False
  • C. None
  • D. Error

Respuesta:

👉 A. True

Cuando hacemos y = x lo que estamos haciendo es crear una nueva variable que apunta a la misma dirección de memoria que la lista x.

La palabra reservada is verifica si dos variables apuntan a la misma dirección de memoria, en este ejemplo es justo este el caso, por ende la respuesta sería True.

Para ver que ambas direcciones de memoria son iguales podemos usar el método id():

x = [1, 2, 3]
y = x
print(id(x)) # 134693457550400
print(id(y)) # 134693457550400
Enter fullscreen mode Exit fullscreen mode

Si quisiéramos crear una copia completamente independiente que este almacenada en otra dicción de memoria tendríamos que usar el método copy().


Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay