Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does (1 in [1,0] == True) evaluate to False?
    text
    copied!<p>When I was looking at answers to <a href="https://stackoverflow.com/questions/9201445/python-best-way-to-keep-track-of-results-from-loop">this question</a>, I found I didn't understand my own answer.</p> <p>I don't really understand how this is being parsed. Why does the second example return False?</p> <pre><code>&gt;&gt;&gt; 1 in [1,0] # This is expected True &gt;&gt;&gt; 1 in [1,0] == True # This is strange False &gt;&gt;&gt; (1 in [1,0]) == True # This is what I wanted it to be True &gt;&gt;&gt; 1 in ([1,0] == True) # But it's not just a precedence issue! # It did not raise an exception on the second example. Traceback (most recent call last): File "&lt;pyshell#4&gt;", line 1, in &lt;module&gt; 1 in ([1,0] == True) TypeError: argument of type 'bool' is not iterable </code></pre> <p>Thanks for any help. I think I must be missing something really obvious.</p> <hr> <p>I think this is subtly different to the linked duplicate:</p> <p><a href="https://stackoverflow.com/questions/6074018/why-does-the-expression-0-0-0-return-false-in-python">Why does the expression 0 &lt; 0 == 0 return False in Python?</a>.</p> <p>Both questions are to do with human comprehension of the expression. There seemed to be two ways (to my mind) of evaluating the expression. Of course neither were correct, but in my example, the last interpretation is impossible.</p> <p>Looking at <code>0 &lt; 0 == 0</code> you could imagine each half being evaluated and making sense as an expression:</p> <pre><code>&gt;&gt;&gt; (0 &lt; 0) == 0 True &gt;&gt;&gt; 0 &lt; (0 == 0) True </code></pre> <p>So the link answers why this evaluates <code>False</code>:</p> <pre><code>&gt;&gt;&gt; 0 &lt; 0 == 0 False </code></pre> <p>But with my example <code>1 in ([1,0] == True)</code> doesn't make sense as an expression, so instead of there being two (admittedly wrong) possible interpretations, only one seems possible:</p> <pre><code>&gt;&gt;&gt; (1 in [1,0]) == True </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload