Bird
0
0

Given a numpy array data = np.array([10, 15, 20, 25, 30]), how can you select elements greater than 12 and less than 28 using boolean indexing?

hard📝 Application Q15 of 15
NumPy - Indexing and Slicing
Given a numpy array data = np.array([10, 15, 20, 25, 30]), how can you select elements greater than 12 and less than 28 using boolean indexing?
Adata[data > 12 and data < 28]
Bdata[(data > 12) & (data < 28)]
Cdata[data > 12 | data < 28]
Ddata[(data > 12) or (data < 28)]
Step-by-Step Solution
Solution:
  1. Step 1: Understand combining conditions with & and parentheses

    In numpy, use & for element-wise AND and wrap each condition in parentheses.
  2. Step 2: Check each option

    data[(data > 12) & (data < 28)] correctly uses & (element-wise AND) with parentheses around each condition. Using Python keywords and/or cause errors because they don't work element-wise on arrays. Using | (bitwise OR) selects elements greater than 12 or less than 28, which is wrong here.
  3. Final Answer:

    data[(data > 12) & (data < 28)] -> Option B
  4. Quick Check:

    Use & with parentheses for AND conditions [OK]
Quick Trick: Use & with parentheses for multiple conditions [OK]
Common Mistakes:
  • Using 'and' or 'or' instead of & or |
  • Not using parentheses around conditions
  • Using | (OR) instead of & (AND) for filtering

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes