Bird
0
0

Identify the error in this numpy comparison code:

medium📝 Debug Q14 of 15
NumPy - Array Operations
Identify the error in this numpy comparison code:
import numpy as np
arr = np.array([1, 2, 3])
result = arr = 2
print(result)
AUsing single = instead of == for comparison
BArray must be converted to list before comparison
Cprint() cannot display numpy arrays
DMissing parentheses in print statement
Step-by-Step Solution
Solution:
  1. Step 1: Check comparison syntax

    The code uses arr = 2 which assigns 2 to arr, not compares. Comparison needs ==.
  2. Step 2: Understand effect of assignment

    Assignment overwrites arr with 2, so print(result) prints 2, not a boolean array.
  3. Final Answer:

    Using single = instead of == for comparison -> Option A
  4. Quick Check:

    Comparison needs ==, not = [OK]
Quick Trick: Use == for comparison, = is assignment [OK]
Common Mistakes:
  • Confusing = and ==
  • Thinking print can't show arrays
  • Believing arrays must convert to lists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes