The storage locations of integer objects in Python are different. Some are pre-allocated memory and are always stored in memory, while others open up space when used.
The reason for saying this, You can take a look at the following code:
a = 5 b = 5 a is b # True a = 500 b = 500 a is b # False
As can be seen from the above code, the integer type 5 always exists, but the integer type 500 does not always exist.
So which integers have pre-allocated memory addresses?
a, b, c = 0, 0, 0 i = 0 while a is b: i += 1 a, b = int(str(i)), int(str(i)) else: print(i) # 打印 257
As we know from the above, non-negative integers less than or equal to 256 (2**8) are always stored (that is to say, their memory addresses are allocated in advance and do not need to be allocated later)
a = -1 b = -1 a is b # False
And negative numbers will not be opened in advance.