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

 更新时间:2021年12月28日 15:48:02   作者:cunchi4221  
在Python语言中,用 != 表示不等于,本文主要介绍了python不等于运算符的具体使用,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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攻防-破解附近局域网WIFI密码实现上网自由

    python攻防-破解附近局域网WIFI密码实现上网自由

    本文将记录学习如何通过 Python 脚本实破解附近局域网 WIFI 密码的暴力破解,随时随地免费蹭网,再也不被WiFi密码困扰,实现蹭网自由
    2021-08-08
  • Django点赞的实现示例

    Django点赞的实现示例

    本文主要介绍了Django点赞的实现示例,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • Python内置函数delattr的具体用法

    Python内置函数delattr的具体用法

    本篇文章主要介绍了Python内置函数delattr的具体用法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • windows系统中python使用rar命令压缩多个文件夹示例

    windows系统中python使用rar命令压缩多个文件夹示例

    这篇文章主要介绍了windows系统中python使用rar命令压缩多个文件夹示例,需要的朋友可以参考下
    2014-05-05
  • Python关于excel和shp的使用在matplotlib

    Python关于excel和shp的使用在matplotlib

    今天小编就为大家分享一篇关于Python关于excel和shp的使用在matplotlib,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • 如何一键升级Python所有包

    如何一键升级Python所有包

    这篇文章主要介绍了如何一键升级Python所有包,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2020-11-11
  • Python自动化测试中yaml文件读取操作

    Python自动化测试中yaml文件读取操作

    这篇文章主要介绍了Python自动化测试中yaml文件读取操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • Python for循环你了解吗

    Python for循环你了解吗

    这篇文章主要为大家介绍了Python for循环,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-01-01
  • Python文件处理的魔法之旅

    Python文件处理的魔法之旅

    Python文件处理在数据持久化、配置管理和日志记录方面发挥着至关重要的作用,通过学习如何读取、写入和修改文件,开发者可以更加高效地处理各种数据和配置文件,需要的朋友可以参考下
    2024-11-11
  • Python报错ImportError: IProgress not found. Please update jupyter and ipywidgets解决

    Python报错ImportError: IProgress not found. Please update

    在使用Jupyter Notebook或JupyterLab进行交互式编程时,我们可能会遇到各种导入错误,本文就来介绍一下Python报错ImportError: IProgress not found. Please update jupyter and ipywidgets解决,感兴趣的可以了解一下
    2024-06-06

最新评论