1

I know:

>>> 1 != 2
True

and:

>>> 1 <> 2
True

but I don't know what the difference between <> and !=

Mohammad Reza
  • 693
  • 9
  • 16

5 Answers5

2

<> is removed from the language in Python3. In Python2, they are the same, but != is preferred.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
0

I'm pretty sure they're interchangeable

Mina Han
  • 671
  • 2
  • 10
  • 17
0

Python 2.7 interprets the two statements exactly the same (as NOTEQUAL). See tokenizer.c.

Also from docs:

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

(from https://docs.python.org/2/library/stdtypes.html#stdcomparisons)

How I read these statements

1 != 2: I read this as 1 is not equal to 2.

Knowing that python supports 1 < 2 < 3 to express inequalities, you can consider <> a shortcut for less than or greater than but not equal.

1 <> 2: I read this as 1 is less than 2 exclusive AND 1 is greater than 2 exclusive which happens to exclude the case x==y where x = y.

Jonathan
  • 5,736
  • 2
  • 24
  • 22
0

They have the same functionality. != is used by convention. The only reason <> still exists is for backward compatibility with older versions of Python.

0

It is the same.

See the documentation: https://docs.python.org/2/library/stdtypes.html#stdcomparisons

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

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
DevShark
  • 8,558
  • 9
  • 32
  • 56