Shouldn't the results be the same? I do not understand.
[True,False] and [True, True]
Out[1]: [True, True]
[True, True] and [True,False]
Out[2]: [True, False]
No, because that's not the way that and
operation works in python. First off it doesn't and
the list items separately. Secondly the and
operator works between two objects and if one of them is False (evaluated as False 1) it returns that and if both are True it returns the second one. Here is an example :
>>> [] and [False]
[]
>>>
>>> [False] and []
[]
>>> [False] and [True]
[True]
x and y
: ifx
is false, thenx
, elsey
If you want to apply the logical operations on all the lists pairs you can use numpy arrays:
>>> import numpy as np
>>> a = np.array([True, False])
>>> b = np.array([True, True])
>>>
>>> np.logical_and(a,b)
array([ True, False], dtype=bool)
>>> np.logical_and(b,a)
array([ True, False], dtype=bool)
1. Here since you are dealing with lists an empty list will be evaluated as False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With