[Tutor] comparison bug in python (or do I not get it?)

Kent Johnson kent37 at tds.net
Fri Feb 29 14:21:09 CET 2008


Hans Fangohr wrote:

> In [2]: 2 in [1,2,3] == True
> Out[2]: False
> 
> Why does [2] return False? Would people agree that this is a bug?

No, not a bug. Don't be too quick to blame your tools!

The equivalent expression is
In [1]: (2 in [1,2,3]) and ([1,2,3]==False)
Out[1]: False

'in' is considered a comparison operator and can be chained with other 
comparisons. For a clearer example, consider
In [2]: 2 < 3 < 4
Out[2]: True

which is not the same as
In [3]: 2 < (3 < 4)
Out[3]: False

or
In [4]: (2 < 3) < 4
Out[4]: True

It is equivalent to
In [5]: (2 < 3) and (3 < 4)
Out[5]: True

See
http://docs.python.org/ref/comparisons.html

Kent


More information about the Tutor mailing list