Logical operation between two Boolean lists

前端 未结 7 2023
终归单人心
终归单人心 2021-02-07 15:58

I get a weird result and I try to apply the and or the or operator to 2 Boolean lists in python. I actually get the exact opposite of what I was expec

相关标签:
7条回答
  • 2021-02-07 16:26

    Your lists aren't comparing each individual value, they're comparing the existence of values in the list.

    For any truthy variables a and b:

    a and b
    > b #The program evaluates a, a is truthy, it evaluates b, b is truthy, so it returns the last evaluated value, b.
    a or b
    > a #The program evaluates a, a is truthy, so the or statement is true, so it returns the last evaluated value, a.
    

    Now, truthy depends on the type. For example, integers are truthy for my_int != 0, and are falsy for my_int == 0. So if you have:

    a = 0
    b = 1
    a or b
    > b #The program evaluates a, a is falsy, so the or statement goes on to evaluate b, b is truthy, so the or statement is true and it returns the last evaluated value b.
    
    0 讨论(0)
提交回复
热议问题