1

I know:

>>> 1 != 2
True

and:

>>> 1 <> 2
True

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

Mohammad Reza
  • 607
  • 6
  • 14

5 Answers5

2

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

unutbu
  • 777,569
  • 165
  • 1,697
  • 1,613
0

I'm pretty sure they're interchangeable

Mina Han
  • 641
  • 2
  • 9
  • 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,576
  • 2
  • 22
  • 21
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
  • 107,083
  • 22
  • 201
  • 376
DevShark
  • 7,878
  • 7
  • 30
  • 54