🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.logical_or()

AI & DATA SCIENCE // np-logical-or

np.logical_or() computes the element-wise logical OR of two arrays, treating each element's truthiness rather than requiring them to already be boolean.

Syntax

np.logical_or(x1, x2)

Deep Dive Course

np.logical_or(a, b) mirrors np.logical_and() exactly, but combines truthiness with OR instead of AND, returning True at each position where at least one of the two inputs is truthy. Like logical_and(), it agrees with the bitwise | operator for genuinely boolean arrays, but diverges for non-boolean numeric arrays, where | performs actual bitwise OR on the integer bits instead of a truthiness-based combination.

1Understanding np.logical_or()

np.logical_or(a, b) mirrors np.logical_and() exactly, but combines truthiness with OR instead of AND, returning True at each position where at least one of the two inputs is truthy. Like logical_and(), it agrees with the bitwise | operator for genuinely boolean arrays, but diverges for non-boolean numeric arrays, where | performs actual bitwise OR on the integer bits instead of a truthiness-based combination.

💡

Combine several conditions with | when you want to select elements matching any one of multiple criteria — remember each condition needs its own parentheses due to operator precedence.

editor.html
import numpy as np

a = np.array([True, True, False, False])
b = np.array([True, False, True, False])
print(np.logical_or(a, b))
localhost:3000

2Practical Example

Here is a real-world application of np.logical_or() showing how it is used in production NumPy code.

editor.html
import numpy as np

arr = np.array([-5, 10, 50, 150])
outliers = arr[(arr < 0) | (arr > 100)]
print(outliers)
localhost:3000

3Best Practices

Follow these guidelines when working with np.logical_or():

1. Use | between two boolean condition arrays for combining alternatives, since it's more concise and equivalent to logical_or() in that specific case

2. Wrap each condition in parentheses when combining multiple comparisons with |, since | has higher precedence than comparison operators in Python

3. Use np.logical_or() explicitly, or convert to bool first, rather than | when the inputs might not already be boolean arrays

⚠️

Tip: Combine several conditions with | when you want to select elements matching any one of multiple criteria — remember each condition needs its own parentheses due to operator precedence.

editor.html
import numpy as np

a = np.array([True, True, False, False])
b = np.array([True, False, True, False])
print(np.logical_or(a, b))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

a = np.array([True, True, False, False])
b = np.array([True, False, True, False])
print(np.logical_or(a, b))
Example 02Advanced Example
import numpy as np

arr = np.array([-5, 10, 50, 150])
outliers = arr[(arr < 0) | (arr > 100)]
print(outliers)

Best Practices

  • Use | between two boolean condition arrays for combining alternatives, since it's more concise and equivalent to logical_or() in that specific case
  • Wrap each condition in parentheses when combining multiple comparisons with |, since | has higher precedence than comparison operators in Python
  • Use np.logical_or() explicitly, or convert to bool first, rather than | when the inputs might not already be boolean arrays

Interview Question

Why is it necessary to wrap each comparison in parentheses when combining conditions with | or &, like checking a value is below one bound or above another?

Hint: Think about Python's operator precedence rules.

In Python, the bitwise operators | and & have higher precedence than the comparison operators < and >, which is the opposite of what most people intuitively expect. Without parentheses, an expression combining two comparisons with | would actually get parsed with the bitwise operator applied first, in a way that doesn't produce the intended boolean condition at all, typically raising a confusing error or producing nonsense results. Explicit parentheses around each comparison force Python to evaluate the comparisons first, exactly as intended, before combining them with the bitwise operator.

Exercises

MediumPractice using np.logical_or() in a real scenario.
View Solution
import numpy as np

a = np.array([True, True, False, False])
b = np.array([True, False, True, False])
print(np.logical_or(a, b))

Frequently Asked Questions

Why is it necessary to wrap each comparison in parentheses when combining conditions with | or &, like checking a value is below one bound or above another?

In Python, the bitwise operators | and & have higher precedence than the comparison operators < and >, which is the opposite of what most people intuitively expect. Without parentheses, an expression combining two comparisons with | would actually get parsed with the bitwise operator applied first, in a way that doesn't produce the intended boolean condition at all, typically raising a confusing error or producing nonsense results. Explicit parentheses around each comparison force Python to evaluate the comparisons first, exactly as intended, before combining them with the bitwise operator.

Related Functions

np-logical-andnp-logical-notboolean-indexing