'!='等于'不是' [英] Does '!=' equivelent to 'is not'

查看:78
本文介绍了'!='等于'不是'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对不是是否有点困惑。等于!=


如果a!= b:

...


if a不是b:

...

不是之间的区别是什么?和!=或者它们是同一个东西?

解决方案

pirata写道:


我'' ma有点混淆是不是等于!=


如果a!= b:

...


if a不是b:

...


"之间的区别是什么?和!=或者他们是一回事?



不,它们不是一回事。 ==和!=测试看看

的*值*两个变量是否相同。像这样:


>> a =''hello world''
b =''hello world''
a == b



True


a和b都具有''你好世界'的价值,所以它们是相等的


是但不是,不测试价值等值,他们测试对于

对象标识。换句话说,他们测试看两个

变量引用的对象是否是内存中的同一个对象,如下所示:


>> a是b



False


a和b被分配给碰巧具有相同价值的两个不同对象,但不过有两个单独的''你好世界''

内存中的对象,因此你不能说a *是* b


现在看看这个:


>> c = d =''hello world''
c == d



True


>> c是d



True


在这种情况下,它们又是相同的值,但现在也是测试

显示它们也是相同的*对象*,因为c和d

在内存中引用相同的hello world对象。只有一个这个

的时间。


!=并且工作原理不一样:


>> a!= b





>> a不是b



True


> > c!= d



False


>> c不是d



False


>>>



希望有所帮助!


pirata写道:


我是否有点混淆是不是等于!=


如果a!= b:

...


if a不是b:

...


"之间的区别是什么?和!=或者他们是一回事?



`==`运算符测试相等性。 `is`操作符测试身份。


如果您不打算测试身份,请使用`==`。如果你不知道身份测试是什么(除了测试

for None-ness),那么你不需要它。 br />

-

Erik Max Francis&& ma*@alcyone.com && http://www.alcyone.com/max/

美国加利福尼亚州圣何塞市&& 37 18 N 121 57 W&& AIM,Y!M erikmaxfrancis

我们问小鸟在唱歌时希望获得多少利润?

- Johannes Kepler,1571-1630


6月16日,10:29 * pm,pirata< pir ... @ mars.invalidwrote:


I''ma对于不是是否有点困惑等于!=


如果a!= b:

* ...


如果a不是b:

* ...


不是之间的区别是什么?和!=或者他们是一回事?



不是是是的逻辑否定。运算符,而!=

==的逻辑否定运营商。不同之处是平等

与身份相比。


>> ; a = [1,2,3]
b = [1,2,3]
a == b



True


>> a是b



False


I''m a bit confusing about whether "is not" equivelent to "!="

if a != b:
...

if a is not b:
...
What''s the difference between "is not" and "!=" or they are the same thing?

解决方案

pirata wrote:

I''m a bit confusing about whether "is not" equivelent to "!="

if a != b:
...

if a is not b:
...
What''s the difference between "is not" and "!=" or they are the same thing?

No, they are not the same thing. == and != test to see if the *value* of
two variables are the same. Like so:

>>a = ''hello world''
b = ''hello world''
a == b

True

a and b both have the value of ''hello world'', so they are equal

is and is not, however, do not test for value equivalence, they test for
object identity. In other words, they test to see if the object the two
variables reference are the same object in memory, like so:

>>a is b

False

a and b are assigned to two different objects that happen to have the
same value, but nevertheless there are two separate ''hello world''
objects in memory, and therefore you cannot say that a *is* b

Now look at this:

>>c = d = ''hello world''
c == d

True

>>c is d

True

In this case, they are again the same value, but now the is test also
shows that they are the same *object* as well, because both c and d
refer to the same ''hello world'' object in memory. There is only one this
time.

!= and is not work the same:

>>a != b

False

>>a is not b

True

>>c != d

False

>>c is not d

False

>>>

Hope that helps!


pirata wrote:

I''m a bit confusing about whether "is not" equivelent to "!="

if a != b:
...

if a is not b:
...
What''s the difference between "is not" and "!=" or they are the same thing?

The `==` operator tests equality. The `is` operator tests identity.

If you don''t specifically intend to test for identity, use `==`. If you
don''t know what identity tests are for (with the exception of testing
for None-ness), then you don''t need it.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
Do we ask what profit the little bird hopes for in singing?
-- Johannes Kepler, 1571-1630


On Jun 16, 10:29*pm, pirata <pir...@mars.invalidwrote:

I''m a bit confusing about whether "is not" equivelent to "!="

if a != b:
* ...

if a is not b:
* ...

What''s the difference between "is not" and "!=" or they are the same thing?

"is not" is the logical negation of the "is" operator, while "!=" is
the logical negation of the "==" operator. The difference is equality
versus identity.

>>a = [1, 2, 3]
b = [1, 2, 3]
a == b

True

>>a is b

False


这篇关于'!='等于'不是'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆