欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python不等于运算符的具体使用

程序员文章站 2022-03-06 13:20:26
python not equal operator returns true if two variables are of same type and have different values,...

python not equal operator returns true if two variables are of same type and have different values, if the values are same then it returns false.

如果两个变量具有相同的类型并且具有不同的值 ,则python不等于运算符将返回true ;如果值相同,则它将返回false 。

python is dynamic and strongly typed language, so if the two variables have the same values but they are of different type, then not equal operator will return true.

python是动态的强类型语言,因此,如果两个变量具有相同的值,但它们的类型不同,则不相等的运算符将返回true 。

python不等于运算符 (python not equal operators)

operator description
!= not equal operator, works in both python 2 and python 3.
<> not equal operator in python 2, deprecated in python 3.
操作员 描述
!= 不是equal运算符,可在python 2和python 3中使用。
<> 在python 2中不等于运算符,在python 3中已弃用。

python 2示例 (python 2 example)

let's see some examples of not-equal operator in python 2.7.

我们来看一些python 2.7中不等于运算符的示例。

$ python2.7
python 2.7.10 (default, aug 17 2018, 19:45:58) 
[gcc 4.2.1 compatible apple llvm 10.0.0 (clang-1000.0.42)] on darwin
type "help", "copyright", "credits" or "license" for more information.
>>> 10 <> 20
true
>>> 10 <> 10
false
>>> 10 != 20
true
>>> 10 != 10
false
>>> '10' != 10
true
>>>

python 3示例 (python 3 example)

here is some examples with python 3 console.

这是python 3控制台的一些示例。

$ python3.7
python 3.7.0 (v3.7.0:1bf9cc5093, jun 26 2018, 23:26:24) 
[clang 6.0 (clang-600.0.57)] on darwin
type "help", "copyright", "credits" or "license" for more information.
>>> 10 <> 20
  file "<stdin>", line 1
    10 <> 20
        ^
syntaxerror: invalid syntax
>>> 10 != 20
true
>>> 10 != 10
false
>>> '10' != 10
true
>>>

we can use python not equal operator withf-strings too if you are using python 3.6 or higher version.

如果您使用的是python 3.6或更高版本,我们也可以将python不等于运算符与f字符串一起使用。

x = 10
y = 10
z = 20
 
print(f'x is not equal to y = {x!=y}')
 
flag = x != z
print(f'x is not equal to z = {flag}')
 
# python is strongly typed language
s = '10'
print(f'x is not equal to s = {x!=s}')

output:

输出:

x is not equal to y = false
x is not equal to z = true
x is not equal to s = true

python不等于自定义对象 (python not equal with custom object)

when we use not equal operator, it calls __ne__(self, other) function. so we can define our custom implementation for an object and alter the natural output.

当我们使用不等于运算符时,它将调用__ne__(self, other)函数。 因此,我们可以为对象定义自定义实现并更改自然输出。

let's say we have data class with fields – id and record. when we are using the not-equal operator, we just want to compare it for record value. we can achieve this by implementing our own __ne__() function.

假设我们有带字段的data类-id和record。 当我们使用不等于运算符时,我们只想比较它的记录值。 我们可以通过实现自己的__ne __()函数来实现这一点。

class data:
    id = 0
    record = ''
 
    def __init__(self, i, s):
        self.id = i
        self.record = s
 
    def __ne__(self, other):
        # return true if different types
        if type(other) != type(self):
            return true
        if self.record != other.record:
            return true
        else:
            return false
 
 
d1 = data(1, 'java')
d2 = data(2, 'java')
d3 = data(3, 'python')
 
print(d1 != d2)
print(d2 != d3)

output:

输出:

false
true

notice that d1 and d2 record values are same but “id” is different. if we remove __ne__() function, then the output will be like this:

请注意,d1和d2记录值相同,但“ id”不同。 如果删除__ne __()函数,则输出将如下所示:

true
true

翻译自: https://www.journaldev.com/25101/python-not-equal-operator

到此这篇关于python不等于运算符的具体使用的文章就介绍到这了,更多相关python不等于运算符内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: python 不等于