is与==
is和==的区别
1. id()
通过id()我们可以查看到⼀一个变量表示的值在内存中的地址.
a1 = 100 b1 = 100 print(id(a1),id(b1)) #140712472404096, 140712472404096 a2 = 222222222222 b2 = 222222222222 print(id(a2),id(b2)) #2714854086672, 2714854086672 a3 = True b3 = True print(id(a3),id(b3)) #140712471869776, 140712471869776 内存地址相同 a4 = "abcdefghijklmnopqrstuvwxyz" b4 = "abcdefghijklmnopqrstuvwxyz" print(id(a4),id(b4)) #2230116006848, 2230116006848 内存地址相同 a5 = "abcdefghijklmnopqrstuvwxyz"*3 b5 = "abcdefghijklmnopqrstuvwxyz"*3 print(id(a5),id(b5)) #2234044245808, 2234044245808 内存地址相同 a6 = "uwidsoididoiwqoiewyueiydihbddwowqfewhufdnjcddududfisdygfiuwqiodmcdncduiosiiennxeniionwmwoiwieuruuewuuukskxsniiwhdhi2wuyrtfrvvufioqkq"*3 b6 = "uwidsoididoiwqoiewyueiydihbddwowqfewhufdnjcddududfisdygfiuwqiodmcdncduiosiiennxeniionwmwoiwieuruuewuuukskxsniiwhdhi2wuyrtfrvvufioqkq"*3 print(id(a6),id(b6)) #1998595213104, 1998595213104 内存地址相同 a7 = ["hello""world"] b7 = ["hello""world"] print(id(a7),id(b7)) #1915401364040, 1915401364104 内存地址不同 a8 = ("hello","world") b8 = ("hello","world") print(id(a8),id(b8)) #2328127912840, 2328127912840 内存地址相同 a9 = {"hello":"你好","world":"世界"} b9 = {"hello":"你好","world":"世界"} print(id(a9),id(b9)) #1975585307384, 1975585307456 内存地址不同
注意(一般情况下): 在py文件中. 如果你只是单纯的定义一个字符串. 那么一般情况下都是会被添加到小数据池中的. 我们可以这样认为: 在使⽤用字符串的时候, python会帮我们把字符串进行缓存, 在下次使用的时候直接指向这个字符串即可. 可以节省很多内存.
==: 判断两端的值是否相等
is: 判断两端的内存地址是否相等
a = "who am i" b = "who am i" print(id(a),id(b)) #2142885576880, 2142885576880 print(a is b) #True print(a == b) #True li1 = ["who","am","i"] li2 = ["who","am","i"] print(id(li1),id(li2)) #1891093340744, 1891093340808 print(li1 is li2) #False print(li1 == li2) #True
无限的我,现在才开始绽放,从东边的第一缕阳光到西边的尽头