473,671 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does '!=' equivelent to 'is not'

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?
Jun 27 '08 #1
23 1706
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!
Jun 27 '08 #2
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
Jun 27 '08 #3
On Jun 16, 10:29*pm, pirata <pir...@mars.in validwrote:
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
Jun 27 '08 #4
John Salerno wrote:
== and != test to see if the *value* of
two variables are the same.
Let me just clarify this. It might seem like a picky point, but I think
it's pretty important when learning Python.

I don't really mean the value of the variables themselves, I mean the
values that the variables refer to. The variables themselves aren't
actually the objects, nor do they have values, exactly. Instead, they
*refer* to objects in memory, and it is these objects that we are
testing the values and identities of. For example, using that previous code:

a ---'hello world'

b ---'hello world'

c ---\
---'hello world'
d ---/

a and b were assigned to two separate objects (it doesn't matter that
they happen to be the same value). As you can see above, a and b refer
to different things.

c and d, however, were assigned simultaneously to the same object, and
therefore refer to a single object in memory. This is why "c is d" is
True, but "a is b" is False.
Jun 27 '08 #5
On Tue, Jun 17, 2008 at 11:29 AM, pirata <pi****@mars.in validwrote:
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 'is' is used to test do they point to the exactly same object.
The '==' is used to test are their values equal.

same objects are equal, but equal don't have to be the same object.

and be very careful to the dirty corner of python:
>>a = 100000
b = 100000
a is b
False
>>a == b
True
>>a = 5
b = 5
a is b
True
>>a == b
True
>>>
which is caused by small object cache mechanism.
--
Best Regards,
Leo Jay
Jun 27 '08 #6
Lie
On Jun 17, 11:07*am, "Leo Jay" <python.leo...@ gmail.comwrote:
On Tue, Jun 17, 2008 at 11:29 AM, pirata <pir...@mars.in validwrote:
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 'is' is used to test do they point to the exactly same object.
The '==' is used to test are their values equal.

same objects are equal, but equal don't have to be the same object.

and be very careful to the dirty corner of python:
No you don't have to be careful, you should never rely on it in the
first place.

Basically 'a is b' and 'not(a is b)' is similar to 'id(a) == id(b)'
and 'not(id(a) == id(b))'

You use 'is' when you want to test whether two variable/names are
actually the same thing (whether they actually refers to the same spot
on memory). The '==' equality comparison just test whether two
objects' values can be considered equal.
Jun 27 '08 #7
En Tue, 17 Jun 2008 02:25:42 -0300, Lie <Li******@gmail .comescribió:
On Jun 17, 11:07*am, "Leo Jay" <python.leo...@ gmail.comwrote:
>On Tue, Jun 17, 2008 at 11:29 AM, pirata <pir...@mars.in validwrote:
What's the difference between "is not" and "!=" or they are the same thing?

The 'is' is used to test do they point to the exactly same object.
The '==' is used to test are their values equal.

and be very careful to the dirty corner of python:
[example with "5 is 5" but "100000 is not 100000"]
No you don't have to be careful, you should never rely on it in the
first place.

Basically 'a is b' and 'not(a is b)' is similar to 'id(a) == id(b)'
and 'not(id(a) == id(b))'
No.
You use 'is' when you want to test whether two variable/names are
actually the same thing (whether they actually refers to the same spot
on memory). The '==' equality comparison just test whether two
objects' values can be considered equal.
Yes, *that* is true. The above statement is not. A counterexample:

py[] is []
False
pyid([])==id([])
True

Even dissimilar objects may have the same id:

pyclass A: pass
....
pyclass B: pass
....
pyA() is B()
False
pyA() == B()
False
pyid(A())==id(B ())
True

Comparing id(a) with id(b) is only meaningful when a and b are both alive at the same time. If their lifetimes don't overlap, id(a) and id(b) are not related in any way. So I think that trying to explain object identity in terms of the id function is a mistake.

--
Gabriel Genellina

Jun 27 '08 #8
"Leo Jay" <py***********@ gmail.comwrote:
same objects are equal, but equal don't have to be the same object.
same objects are often equal, but not always:
>>inf = 2e200*2e200
ind = inf/inf
ind==ind
False
>>ind is ind
True

--
Duncan Booth http://kupuguy.blogspot.com
Jun 27 '08 #9
On Tue, Jun 17, 2008 at 04:33:03AM -0300, Gabriel Genellina wrote:
Basically 'a is b' and 'not(a is b)' is similar to 'id(a) == id(b)'
and 'not(id(a) == id(b))'

No.
Sure it is... he said "similar".. . not identical. They are not the
same, but they are similar.

Saying a flat "no" alone, without qualifying your statement is
generally interpreted as rude in English... It's kind of like how you
talk to children when they're too young to understand the explanation.
Yucky.

--
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFIV6mFdjd lQoHP510RAnuCAJ 0bduRUXrOsAM1Ye VkB6at7QGCMQACd FTD7
qhGWDqFcvX3JokV te+EXu8s=
=OQ51
-----END PGP SIGNATURE-----

Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
1767
by: Helmut Jarausch | last post by:
Hi, what does Python do if two objects aren't comparable (to my opinion) If I've understood "Python in a Nutschell" correctly it should raise an exception but it doesn't do for me. Here are two examples if 2 > '1' : print "greater" else : print "less_or_equal"
10
11193
by: sidewinder | last post by:
If it does, and it is going between a little endian and a big endian machines can we byte swap it as an int or long? Or does the mantissa or exponent cross 2 byte boundarys? Can I just call htonl ( (long) floatvalue))
37
4643
by: Curt | last post by:
If this is the complete program (ie, the address of the const is never taken, only its value used) is it likely the compiler will allocate ram for constantA or constantB? Or simply substitute the values in (as would be required if I used the hideous, evil, much-abused #define :) ----------- const int constantA = 10; static const int constantB = 20;
7
1754
by: binary-nomad | last post by:
Hello, When I do a "AND" in a SQL query, eg. SELECT NAME WHERE SEX="male" AND AGE= "30", MySQL does a sub-search, right? i.e. it does the "WHERE SEX=male" first, and then searches through the list of *those* results to see if AGE=30? Does it do this from left to right? i.e. in the query above, would the table always, and necessarily, get searched for "SEX=male" first, and "AGE=30" second?
74
4015
by: Suyog_Linux | last post by:
I wish to know how the free()function knows how much memory to be freed as we only give pointer to allocated memory as an argument to free(). Does system use an internal variable to store allocated memory when we use malloc(). Plz help......
2
2383
by: Nuno Magalhaes | last post by:
In pages that there is no content length, how does HttpWebResponse knows where the page ends? And what kind of objects/methods does it retrieve? Does it only retrieve the initial page without any images, i.e., does it only makes one "GET / HTTP/1.1\r\n\r\n"? Because I'm doing some projects on a webclient (to measure some statistic times) and the total bytes get byte HttpWebResponse are lower than the total objects I get via socket way. ...
3
1291
by: **Developer** | last post by:
I have a usercontrol that contains two pictureboxes One is on top of the other. The bottom one is the parent of the top one. The top one is transparent. I invalidate the top one often
37
1979
by: Erick-> | last post by:
hello!! I was looking at some code in C... and saw this "exotic" operator |=. first time with it, what exactky does??? thanks in advance. Erick->
6
1220
by: Patient Guy | last post by:
The question in the subject seems plain to me. I wonder if EMCAscript/Javascript developers should even bother adding IE7 even to the clients in which to test script/code. The same goes for any web document, interactive/dynamic or otherwise, developer: does IE7 finally get CSS right, and to what level?
3
8293
by: ghd | last post by:
In Windows XP when does the JVM start (JRE version 1.4 and higher)? And when does it halt? Does the JVM start when we launch a java application (or by executing on the command prompt - java classfile)? And does the JVM halt when the all the java applications on the system end? According to the documentation, the JVM halts in two situations: 1. when you terminate the java application by Ctrl+C and 2. when the 'exit' method of the System...
0
8483
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8927
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7445
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6237
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5703
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4416
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.