Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么不';t这些语句都返回';真的';?_Python_Math_Logic_Theory - Fatal编程技术网

Python 为什么不';t这些语句都返回';真的';?

Python 为什么不';t这些语句都返回';真的';?,python,math,logic,theory,Python,Math,Logic,Theory,因此,我在python中遇到了一些奇怪的行为: >>> 2+1 is 3 True >>> 2000+1 is 2001 False 当使用大整数时,它没有使用正确的逻辑,这是为什么?is如果两个变量指向同一个对象,将返回True。这样就有了id In [21]: id(3) Out[21]: 15538056 In [22]: id(2+1) Out[22]: 15538056 In [23]: id(2001), id(2000+1) Out[23]

因此,我在python中遇到了一些奇怪的行为:

>>> 2+1 is 3
True
>>> 2000+1 is 2001
False

当使用大整数时,它没有使用正确的逻辑,这是为什么?

is
如果两个变量指向同一个对象,将返回
True
。这样就有了
id

In [21]: id(3)
Out[21]: 15538056

In [22]: id(2+1)
Out[22]: 15538056

In [23]: id(2001), id(2000+1)
Out[23]: (52399576, 54526360)