1

Couldn't find much on this. Trying to compare 2 values, but they can't be equal. In my case, they can be (and often are) either greater than or less than.

Should I use:

if a <> b:
   dostuff

or

if a != b:
   dostuff

This page says they're similar, which implies there's at least something different about them.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
Jer_TX
  • 465
  • 4
  • 20
  • Don't know how in Python exactly, but I feel <> should be use for numbers only (not for comapring strings), != for both. – pavel Dec 15 '14 at 07:15
  • Well I think `<>` would take two compression first `a>b` and `a – Tanveer Alam Dec 15 '14 at 07:17
  • My thoughts exactly. in my case, `a` and `b` are both numbers. Both can be null, so <> makes the most sense because I don't care about `b` if it's null. – Jer_TX Dec 15 '14 at 07:17
  • Relevant: http://stackoverflow.com/questions/16749121/what-does-mean-in-python/16749135#16749135 – jamylak Dec 15 '14 at 08:02

2 Answers2

24

Quoting from Python language reference,

The comparison operators <> and != are alternate spellings of the same operator. != is the preferred spelling; <> is obsolescent.

So, they both are one and the same, but != is preferred over <>.

I tried disassembling the code in Python 2.7.8

from dis import dis
form_1 = compile("'Python' <> 'Python'", "string", 'exec')
form_2 = compile("'Python' != 'Python'", "string", 'exec')
dis(form_1)
dis(form_2)

And got the following

  1           0 LOAD_CONST               0 ('Python')
              3 LOAD_CONST               0 ('Python')
              6 COMPARE_OP               3 (!=)
              9 POP_TOP
             10 LOAD_CONST               1 (None)
             13 RETURN_VALUE

  1           0 LOAD_CONST               0 ('Python')
              3 LOAD_CONST               0 ('Python')
              6 COMPARE_OP               3 (!=)
              9 POP_TOP
             10 LOAD_CONST               1 (None)
             13 RETURN_VALUE

Both <> and != are generating the same byte code

              6 COMPARE_OP               3 (!=)

So they both are one and the same.

Note:

<> is removed in Python 3.x, as per the Python 3 Language Reference.

Quoting official documentation,

!= can also be written <>, but this is an obsolete usage kept for backwards compatibility only. New code should always use !=.

Conclusion

Since <> is removed in 3.x, and as per the documentation, != is the preferred way, better don't use <> at all.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • Ok, but what if `b` can't be null? using `<>` sounds to me like both variables must have a value – Jer_TX Dec 15 '14 at 07:16
  • 1
    @JeremyDavis As mentioned in the official documentation, `<>` is another way to say `!=`. So there is absolutely no difference between `<>` and `!=`. But we should prefer `!=`. – thefourtheye Dec 15 '14 at 07:17
  • @JeremyDavis Please check the included disassembled byte codes for the confirmation. – thefourtheye Dec 15 '14 at 07:26
  • 1
    Both variables must have a value either way, or you get a NameError before the operator is evaluated (meaning a variable didn't exist, not that it is null). Python doesn't have null; None is just a value which is unequal to other values. – Yann Vernier Dec 15 '14 at 07:38
  • 3
    Commendable effort put into this answer. However the brief answer is just never use `<>` – jamylak Dec 15 '14 at 08:06
0

Just stick to !=.

<> is outdated! Please check recent python reference manual.

Ehsan
  • 1,338
  • 14
  • 13