Challenge - 5 Problems
Logical Operations Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
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.
✗ Incorrect
np.logical_not(arr1) flips True to False and False to True. So it becomes [False, True, False, True]. Then logical_and with arr2 ([False, False, True, True]) results in True only where both are True, which is only at the last position.
❓ data_output
intermediate1: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)
Attempts:
2 left
💡 Hint
logical_or returns True if either element is True. Count how many positions have True after OR.
✗ Incorrect
logical_or combines elements: [True or False, False or True, False or False, True or False, False or True] = [True, True, False, True, True]. Counting True values gives 4.
🔧 Debug
advanced1: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)
Attempts:
2 left
💡 Hint
Check if the arrays have the same shape for element-wise operations.
✗ Incorrect
NumPy logical operations require arrays to have compatible shapes. Here, arr1 has shape (3,) and arr2 has shape (2,), so they cannot be broadcast together, causing a ValueError.
🚀 Application
advanced2: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]])
Attempts:
2 left
💡 Hint
Use element-wise logical AND with & and parentheses for conditions.
✗ Incorrect
Option B uses element-wise & with parentheses, which is correct for NumPy arrays. Option B is also valid but less common; however, option B uses Python 'and' which does not work element-wise, and option B uses logical_or which is incorrect for the AND condition.
🧠 Conceptual
expert2:00remaining
Understanding short-circuit behavior in NumPy logical operations
Which statement about NumPy logical operations is TRUE?
Attempts:
2 left
💡 Hint
Think about how NumPy processes arrays element-wise.
✗ Incorrect
NumPy logical operations evaluate all elements element-wise and do not short-circuit like Python's built-in 'and' or 'or'. They return an array of results for all elements.