0
0
NumPydata~20 mins

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

Choose your learning style9 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.