Logical operations help us check conditions on data. They tell us if things are true or false, which is useful for filtering and decision-making.
Logical operations (and, or, not) in NumPy
Introduction
Syntax
NumPy
numpy.logical_and(condition1, condition2) numpy.logical_or(condition1, condition2) numpy.logical_not(condition)
Use numpy.logical_and to check if both conditions are true.
Use numpy.logical_or to check if at least one condition is true.
Use numpy.logical_not to reverse a condition.
Examples
NumPy
import numpy as np arr = np.array([1, 2, 3, 4, 5]) cond1 = arr > 2 cond2 = arr < 5 result = np.logical_and(cond1, cond2) print(result)
NumPy
import numpy as np arr = np.array([1, 2, 3, 4, 5]) cond1 = arr == 1 cond2 = arr == 5 result = np.logical_or(cond1, cond2) print(result)
NumPy
import numpy as np arr = np.array([True, False, True]) result = np.logical_not(arr) print(result)
Sample Program
This program shows how to use logical AND, OR, and NOT to filter numbers based on conditions.
NumPy
import numpy as np # Create an array of numbers numbers = np.array([10, 15, 20, 25, 30]) # Condition 1: numbers greater than 15 cond1 = numbers > 15 # Condition 2: numbers less than or equal to 25 cond2 = numbers <= 25 # Find numbers that satisfy both conditions (AND) both_conditions = np.logical_and(cond1, cond2) print('Numbers > 15 AND <= 25:', numbers[both_conditions]) # Find numbers that satisfy either condition (OR) either_condition = np.logical_or(cond1, cond2) print('Numbers > 15 OR <= 25:', numbers[either_condition]) # Find numbers NOT greater than 15 not_condition = np.logical_not(cond1) print('Numbers NOT > 15:', numbers[not_condition])
Important Notes
Logical operations work element-wise on arrays.
Use parentheses to group conditions clearly when combining multiple logical operations.
Summary
Logical operations help combine or reverse conditions on data.
Use logical_and for AND, logical_or for OR, and logical_not for NOT.
They are useful for filtering and selecting data in arrays.
Practice
1. Which NumPy function is used to perform a logical AND operation element-wise on two arrays?
easy
Solution
Step 1: Understand logical AND operation in NumPy
Logical AND combines two boolean arrays and returns True only where both are True.Step 2: Identify the correct NumPy function
numpy.logical_and is the function designed for element-wise logical AND on boolean arrays.Final Answer:
numpy.logical_and -> Option AQuick Check:
AND operation = numpy.logical_and [OK]
Hint: AND operation in NumPy uses logical_and function [OK]
Common Mistakes:
- Confusing bitwise_and with logical_and
- Using logical_or instead of logical_and
- Using and_ which is not a NumPy function
2. Which of the following is the correct syntax to apply a logical NOT operation on a NumPy array
a?easy
Solution
Step 1: Recall the correct function for logical NOT in NumPy
The function is numpy.logical_not(array) to invert boolean values element-wise.Step 2: Check syntax correctness
numpy.logical_not(a) uses correct function and parentheses syntax; others are invalid or incorrect.Final Answer:
numpy.logical_not(a) -> Option CQuick Check:
NOT operation = numpy.logical_not(array) [OK]
Hint: Use logical_not with parentheses for NOT operation [OK]
Common Mistakes:
- Using numpy.not which does not exist
- Using square brackets instead of parentheses
- Trying to negate with not keyword on arrays
3. What is the output of the following code?
import numpy as np x = np.array([True, False, True]) y = np.array([False, False, True]) result = np.logical_or(x, y) print(result)
medium
Solution
Step 1: Understand logical OR operation element-wise
logical_or returns True if either element in the pair is True.Step 2: Apply logical_or to each pair in arrays x and y
Pairs: (True, False) -> True, (False, False) -> False, (True, True) -> True.Final Answer:
[True False True] -> Option DQuick Check:
logical_or([True,False,True],[False,False,True]) = [True,False,True] [OK]
Hint: OR is True if any element is True in pair [OK]
Common Mistakes:
- Confusing logical_or with logical_and
- Misreading boolean values in arrays
- Assuming all False if one element is False
4. Identify the error in the following code snippet:
import numpy as np arr = np.array([True, False, True]) result = arr and np.array([False, True, False]) print(result)
medium
Solution
Step 1: Understand Python 'and' operator with arrays
'and' is a Python keyword that does not work element-wise on arrays; it expects single boolean values.Step 2: Identify correct way to do element-wise logical AND
Use numpy.logical_and(arr1, arr2) for element-wise AND on arrays.Final Answer:
Using 'and' operator directly on NumPy arrays causes an error -> Option BQuick Check:
Python 'and' fails on arrays; use logical_and [OK]
Hint: Use numpy.logical_and, not Python 'and' for arrays [OK]
Common Mistakes:
- Using Python 'and' instead of numpy.logical_and
- Trying to use 'and' without parentheses
- Converting arrays to lists unnecessarily
5. Given two NumPy arrays:
Which code snippet correctly creates a boolean array that is True where
a = np.array([1, 0, 3, 0, 5]) b = np.array([0, 2, 3, 4, 0])
Which code snippet correctly creates a boolean array that is True where
a is non-zero AND b is zero?hard
Solution
Step 1: Translate the condition into logical expressions
We want True where a is not zero AND b is zero, so conditions are a != 0 and b == 0.Step 2: Use numpy.logical_and to combine conditions element-wise
np.logical_and(a != 0, b == 0) returns True only where both conditions hold.Final Answer:
np.logical_and(a != 0, b == 0) -> Option AQuick Check:
AND of (a != 0) and (b == 0) = np.logical_and(a != 0, b == 0) [OK]
Hint: Combine conditions with logical_and for AND filtering [OK]
Common Mistakes:
- Using logical_or instead of logical_and
- Negating conditions incorrectly
- Checking for zeros instead of non-zeros
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
