r/learnpython • u/Arag0ld • 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
r/learnpython • u/Arag0ld • Apr 07 '20
If I say
if x != 5;
print(x)
and
if x is not 5;
print(x)
is there a difference?
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
iswas 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.