Python身份运算符
身份运算符用于对对象执行比较操作,即这些运算符检查两个操作数是否引用相同的对象(具有相同的存储位置)。
以下是身份运算符,
语法:
operand1 is operand2 operand1 is not operand2
Python“是”运算符示例
#Python程序演示 #身份运算符的示例 x = [10, 20, 30] y = [10, 20, 30] z = x #使用==运算符比较值 print("x == y: ", x == y) print("y == z: ", y == z) print("z == x: ", z == x) print() #比较对象 #他们是否在指 #是否相同 print("x is y: ", x is y) print("y is z: ", y is z) print("z is x: ", z is x) print()
输出:
x == y: True y == z: True z == x: True x is y: False y is z: False z is x: True
Python“不是”运算符示例
#Python程序演示 #身份运算符的示例 x = [10, 20, 30] y = [10, 20, 30] z = x #比较值 #使用!=运算符 print("x != y: ", x != y) print("y != z: ", y != z) print("z != x: ", z != x) print() #比较对象 #他们是否在指 #是否相同 print("x is not y: ", x is not y) print("y is not z: ", y is not z) print("z is not x: ", z is not x) print()
输出:
x != y: False y != z: False z != x: False x is not y: True y is not z: True z is not x: False