Python中的is和==的区别
1. is 是比较内存地址id()
1 a = "YongJie" 2 3 b = "YongJie" 4 5 print(id(a)) #2331684108696,a的内存地址 6 7 print(a is b) #判断a的内存地址是否等于b 8 9 True
1.1字符串中如果有特殊字符他们的内存地址就不一样
1 a = "Yong@Jie" 2 b = "Yong@Jie" 3 print(a is b) 4 False
1.2字符串中单个*20以内它们的内存地址一样,单个*21以上内存地址不一致
1.3数字小数据池的范围 -5 ~ 256
1 n = -6 2 n1 = -6 3 print(n is n1) #False 4 5 a = -5 6 a1 = -5 7 print(a is a1) #True 8 9 10 b = 257 11 b1 = 257 12 print(b is b1) #True
2. == 是比较值
例:判断a是否等于b
1 a = "YongJie" 2 3 b = "YongJie" 4 5 print(a == b) #判断a的值是否等于b 6 7 True
2019年11月4日