python身份运算符_Python身份运算符

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,

以下是身份运算符,

OperatorDescriptionsExample
isIt returns True if both operands refer to the same objects; False, otherwise.x is y
is notIt 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身份运算符

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值