here is my code
x = 5
y = 5
print(x is y)
print(id(x))
print(id(y))
and the output is
True
1903991482800
1903991482800
I don't know why x and y have the same location here
please help me illustrate this problom! Thanks!
here is my code
x = 5
y = 5
print(x is y)
print(id(x))
print(id(y))
and the output is
True
1903991482800
1903991482800
I don't know why x and y have the same location here
please help me illustrate this problom! Thanks!
your issue is technically a complicated concept, but I will try to explain it to you in simple terms.
Let's say a number, say '3', is stored in your memory. When you declare a = 3, what the Python interpreter actually does is make that variable 'a' point to the memory location where 3 is stored. So if the number 3 is stored in an address like 'xxyyzz', then the moment you declare a = 3, the variable a points to the memory address 'xxyyzz'. Similarly, when you declare another variable b = 3, what happens is variable 'b' also points to the memory location 'xxyyzz'. The 'is' operator in Python compares the memory addresses of those variables, so you get id(a)==id(b) as True.
Hope this helps!