Bird
0
0

Given a numpy array arr = np.array([10, 15, 20, 25]), how can you create a boolean mask selecting elements greater than 12 and less than 24?

hard📝 Application Q8 of 15
NumPy - Array Operations
Given a numpy array arr = np.array([10, 15, 20, 25]), how can you create a boolean mask selecting elements greater than 12 and less than 24?
A(arr > 12) && (arr < 24)
Barr > 12 and arr < 24
C(arr > 12) & (arr < 24)
Dlogical_and(arr > 12, arr < 24)
Step-by-Step Solution
Solution:
  1. Step 1: Understand element-wise logical operations

    In numpy, '&' is used for element-wise AND, while 'and' or '&&' cause errors.
  2. Step 2: Apply correct syntax

    (arr > 12) & (arr < 24) uses '(arr > 12) & (arr < 24)', which is correct for boolean masking.
  3. Final Answer:

    (arr > 12) & (arr < 24) -> Option C
  4. Quick Check:

    Use '&' for element-wise AND in numpy [OK]
Quick Trick: Use '&' for element-wise AND, not 'and' or '&&' [OK]
Common Mistakes:
  • Using 'and' or '&&' instead of '&'
  • Forgetting parentheses around comparisons
  • Using np.logical_and without import

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes