Search code examples
pythoncomparisonparentheses

How a single parenthesis could change the output?


print(3 in [1, 2, 3] == [1, 2, 3])
#Output: True 

print((3 in [1, 2, 3]) == [1, 2, 3])
#Output: False

I'm just wondering what is happening here.


Solution

  • Because of Python's comparison chaining feature.

    3 in [1, 2, 3] == [1, 2, 3]
    

    is treated as

    (3 in [1, 2, 3]) and ([1, 2, 3] == [1, 2, 3])