0

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]
David
  • 8,113
  • 2
  • 17
  • 36
  • Who said you can do bitwise `and` like that? This is a duplicate of https://stackoverflow.com/questions/47419342/logical-operation-between-two-boolean-lists – FiddleStix Nov 29 '19 at 10:38
  • Possible duplicate of [python and / or operators return value](https://stackoverflow.com/questions/4477850/python-and-or-operators-return-value) – Georgy Nov 29 '19 at 10:39
  • I think so @Georgy, plus a confusion between logical and bitwise operators. – Tim Nov 29 '19 at 10:46
  • See also [Python AND operator on two boolean lists - how?](https://stackoverflow.com/questions/32192163/python-and-operator-on-two-boolean-lists-how) which directly addresses the question – Daniel P Nov 29 '19 at 10:49
  • 4
    Does this answer your question? [Python AND operator on two boolean lists - how?](https://stackoverflow.com/questions/32192163/python-and-operator-on-two-boolean-lists-how) – Tim Nov 29 '19 at 10:50

1 Answers1

1

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

SSharma
  • 951
  • 6
  • 15