Python中的is和==的区别

时间:2022-07-01 13:21:19

Python中的is和==的区别

1. is 是比较内存地址id()

 a = "YongJie"

 b = "YongJie"

 print(id(a))  #2331684108696,a的内存地址

 print(a is b)  #判断a的内存地址是否等于b

 True

1.1字符串中如果有特殊字符他们的内存地址就不一样

 a = "Yong@Jie"
b = "Yong@Jie"
print(a is b)
False

1.2字符串中单个*20以内它们的内存地址一样,单个*21以上内存地址不一致

1.3数字小数据池的范围  -5 ~ 256

  n = -6
n1 = -6
print(n is n1)    #False a = -5
a1 = -5
print(a is a1)    #True b = 257
b1 = 257
print(b is b1)    #True

2. == 是比较值

例:判断a是否等于b

 a = "YongJie"

 b = "YongJie"

 print(a == b)  #判断a的值是否等于b

 True

 2019年11月4日