python身份運算符
Identity operators are used to perform the comparison operation on the objects i.e. these operators check whether both operands refer to the same objects (with the same memory location) or not.
身份運算符用于對對象執行比較操作,即這些運算符檢查兩個操作數是否引用相同的對象(具有相同的存儲位置)。
Following are the identity operators,
以下是身份運算符,
Operator | Descriptions | Example |
---|---|---|
is | It returns True if both operands refer to the same objects; False, otherwise. | x is y |
is not | It returns True if both operands do not refer to the same objects; False, otherwise. | x is not y |
操作員 | 內容描述 | 例 |
---|---|---|
是 | 如果兩個操作數都引用相同的對象,則返回True。 錯誤,否則。 | x是y |
不是 | 如果兩個操作數都不引用相同的對象,則返回True;否則,返回True。 錯誤,否則。 | x不是y |
Syntax:
句法:
operand1 is operand2
operand1 is not operand2
Python“是”運算符示例 (Python 'is' operator example)
# Python program to demonstrate the
# example of identity operators
x = [10, 20, 30]
y = [10, 20, 30]
z = x
# Comparing the values using == operator
print("x == y: ", x == y)
print("y == z: ", y == z)
print("z == x: ", z == x)
print()
# Comparing the objects
# whether they are referring
# the same objects or not
print("x is y: ", x is y)
print("y is z: ", y is z)
print("z is x: ", z is x)
print()
Output:
輸出:
x == y: True
y == z: True
z == x: True
x is y: False
y is z: False
z is x: True
Python“不是”運算符示例 (Python 'is not' operator example)
# Python program to demonstrate the
# example of identity operators
x = [10, 20, 30]
y = [10, 20, 30]
z = x
# Comparing the values
# using != operator
print("x != y: ", x != y)
print("y != z: ", y != z)
print("z != x: ", z != x)
print()
# Comparing the objects
# whether they are referring
# the same objects or not
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()
Output:
輸出:
x != y: False
y != z: False
z != x: False
x is not y: True
y is not z: True
z is not x: False
翻譯自: https://www.includehelp.com/python/identity-operators.aspx
python身份運算符