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
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.