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)
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
来源:https://blog.csdn.net/cunchi4221/article/details/107476042


猜你喜欢
- HTML5本地存储初探(二)完成了数据的本地存储,就要将文件存储也搞定。为了实现文件的本地存储,html5搞了一个叫 manifest 的文
- 下面这段代码能够显示,当前用户所能够看到的所有的用户和表有兴趣的, 可以把每个表的内容加上<% Dim objOraSess
- 开发环境与配置win_x64Ubuntu14.04Python3.xpip安装pymysql模块直接使用pip安装 pip install
- 在图片处理过程中,有时候我们想要确定图片中某一像素的坐标,可以通过下面方法得到。点击运行程序,用鼠标点击我们想要获取坐标的区域,即可获得其坐
- 最近由于单位数据库硬盘空间不足,整理的时候查了许多文章,也进行了测试,整理后得出一些经验供大家参考。首先,在网上看到一篇文章,如何Shrin
- 引言通过前面的文章我们已经了解到OpenCV 是一个用于计算机视觉和机器学习的开源 python 库。它主要针对实时计算机视觉和图像处理。它
- 1. set bakupfolder=F:\backup\ 备份文件存放于目录F:\backup\ (此目录需要事先建好) 2. 默认每个数
- 临近下班的时候,突然想到统计热门文章的问题。以前我所知道的热门文章统计,基本有这么几种:按点击数排序 该方法最大的问题在于热门的文章会越来越
- 由于多种原因,进行安全设置的人们常常不理解数据的真正价值,所以,他们也无法对数据进行合适的保护。将你的数据只限于需要的人访问,并保证访问的人
- Python读写文件模式 1、r 打开只读文件,该文件必须存在。 2、r+ 打开可读写的文件,该文件必须存在。 3、w 打开只写文件,若文件
- 以下是SQL连接代码: 代码如下:Function Open_conn() dim Conn,Strconn&
- 做网页时(其实是网页木马呵呵),最让人烦恼的是自己辛辛苦苦写出来的客户端IE运行的javascript代码常常被别人轻易的拷贝,实在让自己的
- 故障:数据库报错:“MSSQL Server 2000 附加数据库错误823”,附加数据库失败。故障
- 定时将源文件或目录使用WinRAR压缩并自动备份到本地或网络上的主机1.确保WinRAR安装在默认路径或者把WinRAR.exe添加到环境变
- 题目:有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?程序分析:可填在百位、十位、个位的数字都是1、2、3、
- 调用函数:#!/usr/bin/env python3 # -*- coding: utf-8 -*- # 函数调用 >>>
- 看到类似__slots__这种形如__xxx__的变量或者函数名就要注意,这些在Python中是有特殊用途的。__slots__我们已经知道
- 今天在mysql官网上下载了mysql-5.7.18-winx64.zip,并解压;然后按https://www.jb51.net/arti
- 这篇文章主要介绍了Python文本处理简单易懂方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- 本次爬虫思路最最重要的是分析信息接口!!!1. 获取url2. 通过请求拿到响应3. 处理反爬4. 提取信息5. 保存内容本次操练网页htt