r/learnpython • • Apr 07 '20

What's the difference between != and is not?

If I say

if x != 5;
   print(x)

and

if x is not 5;
   print(x)

is there a difference?

332 Upvotes

119 comments sorted by

View all comments

254

u/kberson Apr 07 '20

The == operator compares the value or equality of two objects, whereas the Python is operator checks whether two variables point to the same object in memory. In the vast majority of cases, this means you should use the equality operators == and !=, except when you’re comparing to None.

This applies to is not as well.

53

u/Essence1337 Apr 07 '20

is is appropriate for True and False as well

73

u/Ramast Apr 07 '20

If you have a variable x that can only be True/False then you should check using if x: and if not x

15

u/Essence1337 Apr 07 '20

Fair enough but my point was more that True, False and None all are applications for is. Perhaps you have a variable which can be one of the three then maybe it makes more sense to say is True, is False, is None rather than if x, if not x, if x is None, etc, etc

28

u/JohnnyJordaan Apr 07 '20

It would only make sense if the objective was to make sure another Truethy or Falsey value wouldn't give a false positive, eg

if not x:

is True if x would for example be 0 or [], and False of course. While

if x is False:

would only be True if x is in fact a reference to False and not if it's 0 or [].

None is a separate case, so is None is the only option if None is targeted. If it isn't it's included in the Falsey values like 0 and [].

→ More replies (5)

6

u/DrShocker Apr 07 '20

Personally, even though it works, I would avoid it. You could also do it with small ints (less than 255? I forget the cutoff) because they point to the same object.

The reason I would avoid it is because is was meant to determine whether two objects are the same, so even if these result is the same, the intent is not. I would strive to be clear in my intent rather than just getting the right answer.

→ More replies (3)
→ More replies (11)

2

u/flying-sheep flying-sheep Apr 07 '20

Also if it can be something else, like a possibly empty collection (e.g. a string or list). Truthiness/falsyness is a central part of Python.

Comparing to True/False using is can very rarely be useful when dealing with function parameters and defaults.

1

u/MGoRedditor Apr 07 '20

Also useful for truthy / falsy checks!

1

u/captain_awesomesauce Apr 07 '20

Only if you can guarantee how other people will use your code too.

1

u/66bananasandagrape Apr 08 '20

Quote from PEP 8:

Don't compare boolean values to True or False using ==:

# Correct:
if greeting:

# Wrong:
if greeting == True:

Worse:

# Wrong:
if greeting is True:
→ More replies (2)

2

u/R_Olivaw_Daneel Apr 07 '20

So would == return true if the two objects have the same keys and values, but different references? If so, is it just a shallow check?

1

u/[deleted] Apr 07 '20

[removed] — view removed comment

1

u/R_Olivaw_Daneel Apr 07 '20

Awesome, thank you for this explanation. So in the case of `dict` would the `__eq__` only be shallow checks then?

→ More replies (2)

3

u/unsurestill Apr 07 '20

just a question plz dont downvote me to hell lol

so the "is" operator is the same as "=" operator? because they both point at some object right

13

u/kberson Apr 07 '20

No, not the same. The = is an assignment operator

3

u/unsurestill Apr 07 '20

okay thanks for the fast reply mate. cheers!

8

u/kberson Apr 07 '20

Sorry for the brusque reply, was on mobile at the time. Doing an assignment is not the same as checking for is; an assignment copies one value over to another while is checks to see if two point to the same memory location.

Are you thinking Java? In Java, every object is a pointer and doing an assign makes them both point to the same thing. This is not the case in Python.

2

u/synthphreak Apr 07 '20

+1

Unintuitively for the beginner, = in Python has nothing to do with equality. That’s why == exists.

4

u/bouco Apr 07 '20

Honestly, no one should say "plz dont downvote me" before asking a beginner question.

If thats the case that this community rips beginners for asking questions, then thats a problem. Everyone starts from the same place.

1

u/SnowdenIsALegend Apr 07 '20

Thankyou great explanation

1

u/xelf Elf Apr 07 '20

An example:

print(0 == False) #prints True
print(0 is False) #prints False
→ More replies (18)