When to use 'is not' or '!='


#1

Hi there.

So I'm currently going through the python course and am currently at loops part 4. It has the following instructions:

Fill in the loop condition so the user will be prompted for a choice over and over while choice does not equal 'y' and choice does not equal 'n'.

In previous excercises I've stumbled upon some code like this:

if x not in range(0,5) or y not in range(0,5):
print "Do this stuff"
else:
print "Do that stuff

This made me to try to and use the previous syntax applied to this excercise. But seeing the hint eventually corrected me into using !=.

Now my question: when do you use the is not / not in for conditions and when do you use != because it seems to me you could form the condition so it can work with both.

For some reason I tend to think that the != condition is used for strings and single integers, and the other one to check values in a list.

Could someone confirm or elaborate on this topic to shed some clarity over this? I thank you in advance.


#2

I has the same thoughts.
Stack over flow is a great place to learn more about coding and computer science.
A link to your answer here: http://stackoverflow.com/questions/2209755/python-operation-vs-is-not

if the link doesn't work then here:

"== is an equality test. It checks whether the right hand side and the left hand side are equal objects (according to their eq or cmp methods.)

is is an identity test. It checks whether the right hand side and the left hand side are the very same object. No methodcalls are done, objects can't influence the is operation.

You use is (and is not) for singletons, like None, where you don't care about objects that might want to pretend to be None or where you want to protect against objects breaking when being compared against None."


#3

Thank you.

I do know stackoverflow, however using the right keywords for the querry is essential in finding the right/correct thread. I must say, since I was completing this course and got past the ex., I was trying not to get distracted by other sources and neglected to invest the neccessary time to get the answer myself.

The link was useful and explained quite a few things, however some points are still a bit unclear. It seems to me I'm missing a context. That might be because english is not my native language.

The answer to the question is that they are related to identity vs. equality. As I understand every identity condition, is actually an equality check between the two identities and not the identity' value itself.

If I would bind the same string to a and b
--- > a is b would result in false but a == b is true.

What would help me clarify it more from here on out is the opposite of the example above. When are 2 identity conditions true? Any idea?


#4

Consider the following code:

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

if a == b:
    print "a == b"
else:
    print "It's not the case that a == b."
if a is b:
    print "a is b"
else:
    print "It's not the case that a is b."
if a is c:
    print "a is c"
else:
    print "It's not the case that a is c."

The output of this code is:

a == b
It's not the case that a is b.
a is c

Also, a really helpful site for working through understanding what's happening behind the scenes is this one, which I've pre-loaded with the above code: Visualize this code excecution


#5

Thank you James,

I've added your code to my saved snippets to illustrate the difference. It's so kind of you to take the time for writing this out.