首页 > 编程知识 正文

在Python中,不属于比较运算符的是,python中不等于符号

时间:2023-05-06 06:36:43 阅读:185873 作者:2764

python不等于运算符

pythonnotequaloperatorreturnstrueiftwovariablesareof http://www.Sina.com /,ifthevaluesaresamethenitreturnsfalse。

如果两个变量有same type and have different values,则Python不相等的运算符返回True。 如果值相同,则返回False。

pythonisdynamicandstronglytypedlanguage、soifthetwovariableshavethesamevaluesbuttheyareofdifferenttype、thennotequaloperator

Python是一种动态的强类型语言,如果两个变量具有相同的值,但类型不同,则不相等的运算符返回True。

Python不等于“运算符”OperatorDescription!=Not Equal operator,worksinbothpython2and python3. notequaloperatorinpython 2,deprecated in Python 3.操作符的说明! 可以在Python 2和Python 3中使用,而不是在Equal运算符中使用。 Python 2不再使用等于运算符,而Python 3不再使用。 Python 2采样(Python 2 Example ) let’sseesomeexamplesofnot-equaloperatorinpython 2.7。

让我们看一个不是Python 2.7中的等于运算符的示例。

$python2.7python2.7.10(default,aug 172018,19336045:58 (gcc 4.2.1 compatibleapplellvm 10.0.0 ) clang-110=20真10!=10假' 10 '!=10True Python 3示例(Python 3 Example ) hereissomeexampleswithpython 3控制台。

这是Python 3控制台的示例。

$ python 3.7 python 3.7.0 (v 3.7.0:1 BF 9cc 5093、jun 262018、2:26336024 (clang 6.0 ) clang-600.0.57 ) onda dang=20真10!=10假' 10 '!=10 truewecanusepythonnotequaloperatorwithf-stringstooifyouareusingpython 3.6 orhigherversion。

如果使用的是Python 3.6或更高版本,还可以将Python等于运算符与f字符串一起使用。

x=10y=10z=20 print (f ' xisnotequaltoy={ x!=y} ' ) flag=x!=z print (f ' xisnotequaltoz={ flag } ) pythonisstronglytypedlanguages=' 10 ' print ) f'xisnotequaltos={x!=s} ' ) Output:

输出:

xisnotequaltoy=falsexisnotequaltoz=truexisnotequaltos=true python不等于自定义对象(Python not equal with custom object ) )。 When we use not equal operator,it calls __ne__(self,other ) function.sowecandefineourcustomimplementationforanforanforanoranoranononoranbject

使用等号以外的运算符时,将调用__ne__(self,other )函数。 因此,您可以定义对象的自定义实现,并更改自然输出。

let’ssaywehavedataclasswithfieldsidandrecord.whenweareusingthenot-equal operator,wejustwanttocompareitforrecordvalue

假设您有一个带字段的Data类-id和record。 如果使用非等号运算符,则只比较记录值。 这可以通过实现自己的__ne __ ()函数来实现。

classdata : id=0record=' ' def _ init _ (self,I,s ) : self.id=iself.record=sdef _ ne _ (=type (sef ) )=other.record : returntrueelse : returnfalsed1=data (1,' Java ' ) d2=Data(2) 2,' Java ' ) d3=Data(3)=d3 )输出:

输出:

falsetruenoticethatd1and D2 recordvaluesaresamebut“id”is different.ifweremove _ _ ne _ () function,thentheoutputputwion

请注意,d1和d2的记录值相同,但“id”不同。 如果删除__ne __ ()函数,输出将类似于以下内容:

从truetruegithubrepository.github资源库中签出完整的python脚本和更多的python示例。 翻译自:https://www.journal dev.com/25101/python-not-equal-operator

python不等于运算符

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。