原贴:https://ask.csdn.net/questions/659107
带空格的字符串为啥地址不相等。。。奇怪
小整数从-5到256 id相同指向同一位置(这个有可能跟系统或其他的设置有关,不过默认应该是这样的)
可以用is来判断两个变量是否id相同
可以用==来判断值是否相同如
In [1]: a = 256
In [2]: b = 256
In [3]: a is b
Out[3]: True
In [4]: a == b
Out[4]: True
In [5]: c = 257
In [6]: d = 257
In [7]: c is d
Out[7]: False
In [26]: c == d
Out[26]: True
# 负数
In [20]: a = -5
In [21]: b = -5
In [22]: a is b
Out[22]: True
In [23]: a = -6
In [24]: b = -6
In [25]: a is b
Out[25]: False
# 变量赋值
In [27]: c = 257
In [28]: d = c
In [29]: c is d
Out[29]: True
# 字符串
In [30]: a = 'str'
In [31]: b = 'str'
In [32]: a is b
Out[32]: True
# 带空格的字符串
In [36]: a = 'hello world'
In [37]: b = 'hello world'
In [38]: a is b
Out[38]: False