in
判断单个元素是否在序列中, 对字典来说只能判断key,在不在关系
print("ab" in "abcdefg") print("abc" in ["abc", "ac"]) print([1, 2] in [[1, 2], "3"]) print("abc" in {"abc": 33}) print(33 in {"abc": 33})
is
身份判断,判断内存地址是否相等,是不是关系
nameOne = "BeiMenChuiXue" nameTwo = "QiNiuYun" nameThree = "QiNiuYun" print(nameOne is nameTwo, id(nameOne), id(nameTwo)) print(nameTwo is nameThree, id(nameTwo), id(nameThree))
==
判断值是否相等,等不等关系
nameOne = "BeiMenChuiXue" nameTwo = "QiNiuYun" nameThree = "QiNiuYun" print(nameOne == nameTwo) print(nameTwo == nameThree)
相同点
in is == 返回都是bool类型 True 或 False
注意点
=赋值语句中,对于小段字符串,则直接引用而不是重新生成
name = "北门吹雪" name_two = "北门吹雪" print(name is name_two)