Python之is 和 ==的区别

xiaoxiao2021-03-01  22

"""

变量:     id: 在内存中存储的位置;id(a)     value: ==     type:变量的数据类型; type(a) ==: value, type is: 三个都判断

==:只判断value和type,不判断id

** a is b ====> True, a==b一定相等, 返回True; ** a==b ====> True, a is b 不一定;

PyDev console: starting. import sys; print('Python %s on %s' % (sys.version, sys.platform)) sys.path.extend(['/root/PycharmProjects/day04']) Python 3.6.4 (default, Aug  7 2018, 11:03:35) [GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux a = '1' b = 1 a == b False a = '1' b = a a is b True li = [1,2,3] li1 = li li == li1 True li is li1 True li2 = li.copy() id(li)

"""

变量:     id: 在内存中存储的位置;id(a)     value: ==     type:变量的数据类型; type(a) ==: value, type is: 三个都判断

** a is b ====> True, a==b一定相等, 返回True; ** a==b ====> True, a is b 不一定;

PyDev console: starting. import sys; print('Python %s on %s' % (sys.version, sys.platform)) sys.path.extend(['/root/PycharmProjects/day04']) Python 3.6.4 (default, Aug  7 2018, 11:03:35) [GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux a = '1' b = 1 a == b False a = '1' b = a a is b True li = [1,2,3] li1 = li li == li1 True li is li1 True li2 = li.copy() id(li) 140479313773128 id(li2) 140479313773704   ##注意此时li2和li的type和value是一样的,但id不同 li [1, 2, 3] li2 [1, 2, 3] li == li2 True li is li2 False

"""

变量:     id: 在内存中存储的位置;id(a)     value: ==     type:变量的数据类型; type(a) ==: value, type is: 三个都判断

** a is b ====> True, a==b一定相等, 返回True; ** a==b ====> True, a is b 不一定;

PyDev console: starting. import sys; print('Python %s on %s' % (sys.version, sys.platform)) sys.path.extend(['/root/PycharmProjects/day04']) Python 3.6.4 (default, Aug  7 2018, 11:03:35) [GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux a = '1' b = 1 a == b False a = '1' b = a a is b True li = [1,2,3] li1 = li li == li1 True li is li1 True li2 = li.copy() id(li) 140198653811208 id(li2) 140198653810120 li [1, 2, 3] li2 [1, 2, 3] li == li2 True li is li2 False

"""

 

转载请注明原文地址: https://www.6miu.com/read-4150017.html

最新回复(0)