+ 2

[SOLVED] Please explain this statement

print(False== False in [False]) # why output is True ?

12th Feb 2022, 3:42 AM
Ratnapal Shende
Ratnapal Shende - avatar
4 Respuestas
+ 5
Because of Python's comparison chaining feature. False== False in [False] is treated as (False == False) and (False in [False]) which returns True.
12th Feb 2022, 3:56 AM
Simba
Simba - avatar
+ 2
Erik Are you familiar with 1 < x < 10? It's the same thing.
19th Feb 2022, 10:49 AM
Simba
Simba - avatar
+ 1
Fascinating, and not obvious at first glance. In other words, these 3 Python expressions: a == b == c (a == b) == c a == (b == c) ...mean 3 different things. In particular, the first one actually means: (a == b) and (b == c) Thanks Simba
18th Feb 2022, 7:05 PM
Erik
+ 1
Yeah the 1 < x < 10 form is familiar, but it somehow seemed different in my head with equality (rather than inequality) comparisons. I think maybe it was my confusion with this similar-looking (but quite different) idiom: x = y == 10 ...where you do an assignment of a boolean value (to x) based on a comparison (of y).
19th Feb 2022, 3:34 PM
Erik