Python boolean operation

Should the results not match? I do not understand.

[True,False] and [True, True] Out[1]: [True, True] [True, True] and [True,False] Out[2]: [True, False] 
+5
source share
1 answer

No, because this is not what works and in python. Firstly, these are not and list items separately. Secondly, the and operator works between two objects, and if one of them is False ( evaluated as False 1 ), it returns, and if both are True, then returns the second. Here is an example:

 >>> [] and [False] [] >>> >>> [False] and [] [] >>> [False] and [True] [True] 

x and y : if x false, then x , else y

If you want to apply logical operations to all pairs of lists, 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) 

<sub> 1. Here, since you are dealing with lists, an empty list will be evaluated as False Sub>

+2
source

Source: https://habr.com/ru/post/1245767/


All Articles