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

作者:cunchi4221 时间:2022-03-29 11:21:21 

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)

OperatorDescription
!=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

来源:https://blog.csdn.net/cunchi4221/article/details/107476042

标签:python,不等于
0
投稿

猜你喜欢

  • python实现删除列表中某个元素的3种方法

    2023-02-08 16:01:59
  • python 执行文件时额外参数获取的实例

    2022-09-24 05:46:54
  • Python反爬虫技术之防止IP地址被封杀的讲解

    2022-05-09 10:44:12
  • 简单了解python的break、continue、pass

    2022-06-01 01:20:10
  • MySQL的存储过程写法和Cursor的使用

    2008-12-03 15:55:00
  • Python特效之文字成像方法详解

    2021-08-09 09:34:06
  • Python编程之微信推送模板消息功能示例

    2022-11-15 03:45:04
  • Python命令行参数定义及需要注意的地方

    2022-09-16 02:34:39
  • 20行代码教你用python给证件照换底色的方法示例

    2023-04-03 23:48:32
  • OpenCV 边缘检测

    2023-08-19 22:54:20
  • Python实现感知器模型、两层神经网络

    2021-11-14 07:34:19
  • 浅谈Pycharm的项目文件名是红色的原因及解决方式

    2021-07-02 12:57:22
  • Python3实现打格点算法的GPU加速实例详解

    2022-05-26 23:20:30
  • 使用pickle存储数据dump 和 load实例讲解

    2023-05-19 18:50:18
  • Python3.8安装Pygame教程步骤详解

    2022-05-11 15:26:15
  • SQL Servr 2008空间数据应用系列四:基础空间对象与函数应用

    2011-02-23 15:01:00
  • 编写Python的web框架中的Model的教程

    2022-09-29 22:54:19
  • 火遍全网的Python二次元特效轻松掌握

    2022-01-29 22:01:32
  • python中单双下划线的区别对比分析

    2023-12-18 23:58:45
  • 用CSS设置表格Table的细边框的比较好用的方法

    2010-09-06 14:58:00
  • asp之家 网络编程 m.aspxhome.com