Reputation: 17
I did the following in the python shell. The first and third output is correct but the second and is just wrong. I know i can use the zip function to do this but i want to know why python does this.
>>> [1,1,1,1] and [1,0,0,0]
[1, 0, 0, 0]
>>> [1,0,0,0] and [1,1,0,0]
[1, 1, 0, 0]
>>> [1,1,1,1] and [0,0,0,0]
[0, 0, 0, 0]
Upvotes: 0
Views: 2516
Reputation: 953
As mentioned at : Python AND operator on two boolean lists - how?
"and simply returns either the first or the second operand, based on their truth value. If the first operand is considered false, it is returned, otherwise the other operand is returned." by Martijn Pieters
[1,1,1,1] and [1,0,0,0]
=> [1, 0, 0, 0] which is second operand while first is true.
Another example:
a=2
print(a==3 and [1,1,0,0])
return False
while a==3 is false
And
a=2
print(a==2 and [1,1,0,0])
return [1,1,0,0]
while a==2 is true
Upvotes: 1