Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the <> operator do in python?

Tags:

python

I just came across this here, always used like this:

if string1.find(string2) <> -1:
    pass

What does the <> operator do, and why not use the usual == or in?

Sorry if that has been answered before, search engines don't like punctuation.

like image 898
tom Avatar asked Nov 24 '10 01:11

tom


People also ask

What does <> mean in Python?

It means not equal to. It was taken from ABC (python's predecessor) see here: x < y, x <= y, x >= y, x > y, x = y, x <> y, 0 <= d < 10. Order tests ( <> means 'not equals')

What is the meaning of the <= operator?

The less than or equal operator ( <= ) returns true if the left operand is less than or equal to the right operand, and false otherwise.


2 Answers

http://docs.python.org/reference/expressions.html#notin says:

The [operators] <> and != are equivalent; for consistency with C, != is preferred. [...] The <> spelling is considered obsolescent.

like image 189
zwol Avatar answered Sep 20 '22 22:09

zwol


<> is the same as != although the <> form is deprecated. Your code sample could be more cleanly be written as:

if string2 not in string1:
    pass
like image 20
fmark Avatar answered Sep 19 '22 22:09

fmark