is
and ==
.¶When programmng, you will often want to compare things. However, the behaviour of the python interpreter doesn't always meet expectations.
x = 5
y = 5
x == 5
True
x is y
True
a = [0, 1, 2]
b = [0, 1, 2]
a == b
True
a is b
False
b = a
print(a)
print(b)
[0, 1, 2] [0, 1, 2]
a == b
True
a is b
True
a[0] = 100
print(b[0])
100
b = a[:]
b == a
True
b is a
False
x = 1e5
print(x)
100000.0
y = x
x == y
True
x is y
True
x += 1
print(x)
print(y)
100001.0 100000.0
x is y
False
x == y
False
x = 1000
y = 1000
x == y
True
x is y
False
y = int(y)
x == y
True
x is y
False
x = 5.0
y = 5
x == y
True
x is y
False