Hi guys!

sorry if this question has been asked b4.

I just wanna know what is the difference between a==b and a is b?

Thanks

If you have two lists like
list1 = [1, 2, 3]
list2 = [1, 2, 3]
then
list1 == list2 is True, they have equal content
but
list1 is list2 would give False, they are not the same object.

Hi guys!

sorry if this question has been asked b4.

I just wanna know what is the difference between a==b and a is b?

Thanks

Thank you very much for this exellent reply

I have also notice that if you do id(list1) and id(list2) they are both stored in different memory location.

Now, a point to note is that when you do something like

>>> a = "string"
>>> b = "string"
>>> id(a)
11147264
>>> id(b)
11147264

AND observe this one

>>> a = "string"
>>> b = "string "
>>> id(a)
11147264
>>> id(b)
12922656
>>>

I dont understand.. does in the first situation, is b pointing to a, unlike in the second case where b has a white space at the end..

Any idea, how python manage this???

Thanks

>>> a = "string"
>>> b = "string"
>>> id(a)
11147264
>>> id(b)
11147264

"string" is really the object here, an immutable object, so both a and b reference the same object. Now, if you use "string ", then string with a space is a different and new object.

In the case with the two lists, you have a mutable object that can be changed in place, like you could reverse list1 but not list2. For that reason list1 and list2 have different references.

This question has already been answered. Start a new discussion instead.