Why does “not(True) in [False, True]” return False?

前端 未结 8 1289
清酒与你
清酒与你 2020-12-04 05:29

If I do this:

>>> False in [False, True]
True

That returns True. Simply because False is in the list.

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-04 05:39

    Let's see it as a collection containment checking operation: [False, True] is a list containing some elements.

    The expression True in [False, True] returns True, as True is an element contained in the list.

    Therefore, not True in [False, True] gives the "boolean opposite", not result of the above expression (without any parentheses to preserve precedence, as in has greater precedence than not operator). Therefore, not True will result False.

    On the other hand, (not True) in [False, True], is equal to False in [False, True], which is True (False is contained in the list).

提交回复
热议问题