Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this boolean expression evaluate to False? [duplicate]

Tags:

python

Can someone explain to me why the Python interpreter evaluates this expression to be False?

1 in [1] == True

I would expect that 1 in [1] would evaluate to True, and obviously True == True would be True. However this isn't what happens - the expression is False. Why does this happen?

like image 473
rickyrichboy Avatar asked Dec 13 '22 11:12

rickyrichboy


1 Answers

== and in are both comparison operators. And when you have multiple comparison operators like this, Python considers it a chained comparison. For example, 1 < x < 10 is equivalent to 1 < x and x < 10.

In your case, 1 in [1] == True is equivalent to (1 in [1]) and ([1] == True), which evaluates to False.

like image 183
interjay Avatar answered Jan 04 '23 22:01

interjay