[Tutor] comparison bug in python (or do I not get it?)
Hans Fangohr
h.fangohr at soton.ac.uk
Fri Feb 29 14:42:43 CET 2008
Hi Kent,
> 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!
That's good news. I'd be worried if this wasn't the desired behaviour
-- I just hadn't understood the logic.
>
> The equivalent expression is
> In [1]: (2 in [1,2,3]) and ([1,2,3]==False)
> Out[1]: False
Ah -- that's the way to read it!
>
> '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
>
Well explained -- makes perfect sense now.
Many thanks,
Hans
> See
> http://docs.python.org/ref/comparisons.html
>
> Kent
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>
--
Hans Fangohr
School of Engineering Sciences
University of Southampton
Phone: +44 (0) 238059 8345
Email: fangohr at soton.ac.uk
http://www.soton.ac.uk/~fangohr
More information about the Tutor
mailing list