What is the numpy.logical_or() method in Python?

Overview

The numpy.logical_or() method is used to calculate truth values between x1 and x2 element-wise. The logical OR returns true, when at least one input is true. In mathematical terms, it is represented with v. Here, we have a truth table for the OR operation between p and q.

OR truth table

Syntax


numpy.logical_or(x1,
x2,
/,
out=None,
*,
where=True,
casting='same_kind',
order='K',
dtype=None,
subok=True)

Parameters

  • x1, x2: These are array-like objects that are used to apply logical OR between input values. If x1 and x2 dimensions are not alike, they are broadcasted to a common shape.
  • out: These are locations in memory used to store results. It can be an Ndarray or tuple of Ndarray or None.
  • where: When a location becomes true, the out array is set to ufunc.
  • casting: Its default value is 'same_kind', meaning object casting will be the same as float32 or float64.
  • **kwargs: These are more argument values as keywords.

Return value

This method returns a single boolean value when x1 and x2 are boolean values. When x1 & x2 are boolean Ndarray, it returns a broadcasted boolean Ndarray.

Explanation

In the code snippet, we're going to evaluate logical OR between two scalar values, two boolean arrays, and two logical conditions in lines 4, 6 and 11, respectively.

# import numpy library in program
import numpy as np
# invoking logical or on two boolean values
print("x1 v x2 between boolean values: ", np.logical_or(1, 0))
# invoking logical or on two boolean arrays
print("x1 v x2 between boolean arrays: ", np.logical_or(np.array(
[True, True, False, False, True]), np.array([True, False, True, False, False])))
# creating a numpy array from 0 to 7
x = np.arange(7)
# print logical_or() results
print("x1 v x2 between conditions: ", np.logical_or(x < 1, x > 3))

    Free Resources