Why is (1 in [1,0] == True) False?

Asked 1 years ago, Updated 1 years ago, 52 views

I didn't know how (1 in [1,0] == True) could be parsed, so I tried it in my own way.

>>> 1 in [1,0] # That's how it's supposed to be.
True
>>> 1 in [1,0] == True #Strange
False
>>> (1 in [1,0]) == True # I thought it would be parsed like this
True
>>> 1 in ([1,0] == True) # Error
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    1 in ([1,0] == True)
TypeError: argument of type 'bool' is not iterable

syntax python operation-precedence

2022-09-21 19:30

1 Answers

Python is usually chained comparisons when you look at these phrases.

(1 in [1,0] == True) is actually

(1 in [1, 0]) and ([1, 0] == True)

It's the same thing that makes it false.

For more information

You have already answered this question [Why does Python (0 < 0 == 0) return False?]


2022-09-21 19:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.