Bird
Raised Fist0
NumPydata~20 mins

Logical operations (and, or, not) in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Logical Operations Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of combined logical operations on NumPy arrays
What is the output of this code snippet using NumPy logical operations?
NumPy
import numpy as np
arr1 = np.array([True, False, True, False])
arr2 = np.array([False, False, True, True])
result = np.logical_and(np.logical_not(arr1), arr2)
print(result)
A[False False True False]
B[False True False False]
C[True False False False]
D[False False False True]
Attempts:
2 left
💡 Hint
Remember that logical_not flips True to False and vice versa, then logical_and returns True only if both are True.
❓ data_output
intermediate
1:30remaining
Count of True values after logical OR operation
Given two boolean NumPy arrays, what is the count of True values after applying logical OR?
NumPy
import numpy as np
arr1 = np.array([True, False, False, True, False])
arr2 = np.array([False, True, False, False, True])
result = np.logical_or(arr1, arr2)
count_true = np.sum(result)
print(count_true)
A4
B3
C5
D2
Attempts:
2 left
💡 Hint
logical_or returns True if either element is True. Count how many positions have True after OR.
🔧 Debug
advanced
1:30remaining
Identify the error in logical operation code
What error does this code raise when run?
NumPy
import numpy as np
arr1 = np.array([True, False, True])
arr2 = np.array([False, True])
result = np.logical_and(arr1, arr2)
print(result)
AIndexError: index out of bounds
BTypeError: unsupported operand type(s) for &: 'bool' and 'bool'
CValueError: operands could not be broadcast together with shapes (3,) (2,)
DNo error, outputs [False False False]
Attempts:
2 left
💡 Hint
Check if the arrays have the same shape for element-wise operations.
🚀 Application
advanced
2:30remaining
Filter rows in a NumPy array using logical conditions
Which option correctly filters rows where the first column is greater than 5 AND the second column is less than 10?
NumPy
import numpy as np
data = np.array([[6, 9], [4, 11], [7, 8], [5, 10]])
Adata[np.logical_and(data[:,0] > 5, data[:,1] < 10)]
Bdata[(data[:,0] > 5) & (data[:,1] < 10)]
Cdata[np.logical_or(data[:,0] > 5, data[:,1] < 10)]
Ddata[data[:,0] > 5 and data[:,1] < 10]
Attempts:
2 left
💡 Hint
Use element-wise logical AND with & and parentheses for conditions.
🧠 Conceptual
expert
2:00remaining
Understanding short-circuit behavior in NumPy logical operations
Which statement about NumPy logical operations is TRUE?
ANumPy logical operations always evaluate all elements in the arrays without short-circuiting.
BNumPy logical operations only work on 1D arrays and raise errors on higher dimensions.
CNumPy logical operations raise an error if any element is False in the input arrays.
DNumPy logical operations like np.logical_and short-circuit and stop evaluating as soon as the result is known.
Attempts:
2 left
💡 Hint
Think about how NumPy processes arrays element-wise.

Practice

(1/5)
1. Which NumPy function is used to perform a logical AND operation element-wise on two arrays?
easy
A. numpy.logical_and
B. numpy.and_
C. numpy.bitwise_and
D. numpy.logical_or

Solution

  1. Step 1: Understand logical AND operation in NumPy

    Logical AND combines two boolean arrays and returns True only where both are True.
  2. Step 2: Identify the correct NumPy function

    numpy.logical_and is the function designed for element-wise logical AND on boolean arrays.
  3. Final Answer:

    numpy.logical_and -> Option A
  4. Quick 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
A. numpy.logical_not[]
B. numpy.not(a)
C. numpy.logical_not(a)
D. not numpy.logical(a)

Solution

  1. Step 1: Recall the correct function for logical NOT in NumPy

    The function is numpy.logical_not(array) to invert boolean values element-wise.
  2. Step 2: Check syntax correctness

    numpy.logical_not(a) uses correct function and parentheses syntax; others are invalid or incorrect.
  3. Final Answer:

    numpy.logical_not(a) -> Option C
  4. Quick 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
A. [False False False]
B. [False True True]
C. [True True True]
D. [True False True]

Solution

  1. Step 1: Understand logical OR operation element-wise

    logical_or returns True if either element in the pair is True.
  2. Step 2: Apply logical_or to each pair in arrays x and y

    Pairs: (True, False) -> True, (False, False) -> False, (True, True) -> True.
  3. Final Answer:

    [True False True] -> Option D
  4. Quick 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
A. np.array should be np.logical_and
B. Using 'and' operator directly on NumPy arrays causes an error
C. Missing parentheses around arrays
D. Arrays must be converted to lists before logical operations

Solution

  1. 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.
  2. Step 2: Identify correct way to do element-wise logical AND

    Use numpy.logical_and(arr1, arr2) for element-wise AND on arrays.
  3. Final Answer:

    Using 'and' operator directly on NumPy arrays causes an error -> Option B
  4. Quick 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:
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
A. np.logical_and(a != 0, b == 0)
B. np.logical_or(a != 0, b == 0)
C. np.logical_not(np.logical_and(a == 0, b != 0))
D. np.logical_and(a == 0, b == 0)

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    np.logical_and(a != 0, b == 0) -> Option A
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard