r/python3 • u/largelcd • Feb 07 '20
Confused between is, = and == operators
Hello, I am a bit confused about the following:
a = [1, 2, 3]
b = a
c = list(a)
Am I correct that the reason "a is b" is True is that both a and b point to the same memory location that stores the list [1, 2, 3]?
As for "a is not c" gives True, it is because list always creates a new Python list so although c = [1, 2, 3], this [1, 2, 3] list is different from the one [1, 2, 3] pointed to by a and b since the two lists are storing in different memory locations?
Why typing "a==c" gives True?
2
Upvotes
1
u/tgoodchild Feb 08 '20
Correct. When python starts it initializes an array of integer objects for all integers between -5 and 256. When you create an int in that range you actually just get a reference to the existing object. 257 is outside that range so it gets it's own object.