As far as I know, is
operator in python returns True only if both objects evaluate the same thing in the memory, and ==
returns True both operands values are equal.
>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> a is b
False
>>> a == b
True
So, the code above executes like that.
>>> a = "Text"
>>> b = "Text"
>>> c = "".join(['T', 'e', 'x', 't'])
>>> a is b
True
>>> a is c
False
>>> a == c
True
What is going on with this one? I have used id()
function to check their adresses, and it returned the exact same value for a
and b
but c
returned something different. Why do a
and b
point same thing in memory but c
points something different?
And as a bonus question: how does python find the value of string, number literals. I mean; are the addresses of literals 0, 1, 2, 3, 4, ...
or 'a', 'b', 'c', 'd' ...
the same for different programs? Thanks in advance and I'm sorry if I've used wrong terminology.