Bird
0
0

Given two NumPy arrays:

hard📝 Application Q15 of 15
NumPy - Array Operations
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?
Anp.logical_and(a != 0, b == 0)
Bnp.logical_or(a != 0, b == 0)
Cnp.logical_not(np.logical_and(a == 0, b != 0))
Dnp.logical_and(a == 0, b == 0)
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes