Can we use the Boolean operators or/and for combining Numpy logical statements?


Can we use the Boolean operators or/and to combine multiple logical statements in Numpy?


No, when using Numpy, the operators & (and) and | (or) must be used when combining multiple logical statements.

If we try to apply the operators and or or instead, it will result in a ValueError, because they are not applicable within the context of Numpy arrays and boolean statements.

At a deeper level, & is actually short for the bitwise_and() function, and | is short for the bitwise_or() function in Numpy. Both ways do the same thing, comparing two arrays and returning a list of True or False values.


# | is short for bitwise_or()
(a > 5) | (a < 2)
np.bitwise_or((a > 5), (a < 2))

# & is short for bitwise_and()
(a > 5) & (a < 2)
np.bitwise_and((a > 5), (a < 2))