Logical operators

5.9. Logical operators#

We already learned Python’s logical operators not, or, and and. Unfortunately, these operators work only on booleans, not on arrays of booleans. However, NumPy provides equivalent operators:

Python’s operator on bool

NumPy’s equivalent operator for arrays of bool

not

~

or

|

and

&

Fig. 5.8 illustrates the behaviour of each operator.

Logical operators on Python and Numpy

Fig. 5.8 Correspondence between logical operations on bool and ndarray.#