I found an interesting thing of using "is" to judge whether two variable are same. Here are my code and results:
a = 256
b = 256
c = 257
d = 257
print(a is b)
print(c is d)
and the results show:
True
False
Actually, when the number is bigger than 256, it turns 'False'.
Another example:
a = 'a' * 20
b = 'a' * 20
c = 'a' * 21
d = 'a' * 21
print(a is b)
print(c is d)
Results:
True
False
Similarly, when n in "'a' * n" is bigger than 20, it turns 'False'.
But when I put this into a function, it doesn't matter how big is the number:
def is_test(a):
x = a
y = a
print(x is y)
is_test(257)
is_test('a' * 21)
the results are always 'True'
I ran the code on Jupyter Notebook. Anyone can explain this to me? Thanks