Why a is b can be False when a == b
Equal means the contents match; identical means one object. Two equal lists built separately are not identical, and 2 == 2.0 is True across types.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
Caches, deduplication and 'has this changed' checks all hinge on which question you are asking. Ask identity when you meant equality and things that are equal look different.
How to think about it
Draw the boxes: how many objects exist? Equal compares what is inside the boxes; is compares whether there is one box or two.
Worked example
x = [1, 2]Box one.
y = [1, 2]Box two, same contents.
print(x == y, x is y)True False: same inside, two boxes.
print(2 == 2.0)True: an int and a float with the same value.
z = xAnother name on box one.
z.append(3)Change it through z.
print(x)[1, 2, 3]: z and x are one box, so the append shows through.
Your turn
Are these two records the same data?
same_data = rec1 rec2
Solve one with the tests running
The trap
'a' is 'a' is often True because Python reuses short strings. Code that relied on it works until the strings get longer. Compare text with ==.
Practise == vs is on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.