Course Name: Python for Trading: Basic, Section No: 2, Unit No: 3, Unit type: Quiz
The answer is wrong. The ‘x’ and ‘y’ point to two different int objects. If I change ‘x’ to 101, ‘y’ does not change.
Course Name: Python for Trading: Basic, Section No: 2, Unit No: 3, Unit type: Quiz
The answer is wrong. The ‘x’ and ‘y’ point to two different int objects. If I change ‘x’ to 101, ‘y’ does not change.
Hello Govinda, That’s a common misunderstanding.
When you write:
x = 100
y = 100
Both x and y point to the same int object initially, because of integer interning in Python for small integers (typically from -5 to 256). You can verify this using:
x = 100
y = 100
print(id(x), id(y)) # Will show the same memory address
print(x is y) # Will print True
Now, when you type:
x = 101
You are not changing the value of the object x was pointing to, but instead, you are reassigning x to a new object (the integer 101). This has no effect on y, which is still pointing to the original 100 object.
So,
x = 100 and y = 100 → both point to same object.
Reassigning x = 101 changes x’s reference, not the object y points to.
So the original answer (Option a) is correct.
Hope this is clear. Please let us know if you have any more queries on this.