python中is和 的区别_Python中is和==的区别
"""
is和 ==的區別
"""
a = 'abc'
b = 'abc'
print("都是字符串-a is b--", a is b)
print("都是字符串-a == b--", a == b)
c = 11
d = 11
print("都是數字-c is d--", c is d)
print("都是數字-c == d--", c == d)
e = [0, 1, 2]
f = [0, 1, 2]
print("都是列表-e is f--", e is f)
print("都是列表-e == f--", e == f)
g = h = [0, 1, 2]
print("都是列表-g is f--", g is h)
print("都是列表-g == f--", g == h)
i = (0, 1, 2)
j = (0, 1, 2)
print("都是元祖-i is j--", i is j)
print("都是元祖-i == j--", i == j)
k = {"name": "張三"}
m = {"name": "張三"}
print("都是字典-k is m--", k is m)
print("都是字典-k == m--", k == m)
print("a的id--", id(a))
print("b的id--", id(b))
print("c的id--", id(c))
print("d的id--", id(d))
print("e的id--", id(e))
print("f的id--", id(f))
print("g的id--", id(g))
print("h的id--", id(h))
print("i的id--", id(i))
print("j的id--", id(j))
print("k的id--", id(k))
print("m的id--", id(m))
輸出結果:
image.png
總結:is是判斷id是否相等,==是判斷數據是否相等,如果兩個變量的值是字符串,數字,元祖,那么is和==的結果無區別。如果是列表和字典,is是false,==是true,特例 g = h = [0, 1, 2]這樣的列表,is也是true,因為指向同一空間地址
總結
以上是生活随笔為你收集整理的python中is和 的区别_Python中is和==的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ecshop模板中使用php,使ecsh
- 下一篇: html添加子节点方法,HTML DOM