Bird
Raised Fist0
NumPydata~20 mins

Comparison operations in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Comparison Operations Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of element-wise comparison with numpy arrays
What is the output of this code snippet using numpy arrays for comparison?
NumPy
import numpy as np
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([2, 2, 2, 2])
result = arr1 > arr2
print(result)
A[False False True True]
B[True False False False]
C[True True True True]
D[False False False False]
Attempts:
2 left
💡 Hint
Compare each element of arr1 with the corresponding element in arr2.
❓ data_output
intermediate
1:30remaining
Count of elements equal to a value in numpy array
Given a numpy array, how many elements are equal to 5?
NumPy
import numpy as np
arr = np.array([5, 3, 5, 7, 5, 2])
count = np.sum(arr == 5)
print(count)
A3
B2
C1
D4
Attempts:
2 left
💡 Hint
Use element-wise comparison and sum the True values.
🔧 Debug
advanced
2:00remaining
Identify the error in numpy comparison code
What error does this code raise?
NumPy
import numpy as np
arr = np.array([1, 2, 3])
result = arr > 2 > 1
print(result)
ANo error, outputs [False True True]
BTypeError: '>' not supported between instances of 'numpy.ndarray' and 'int'
CSyntaxError: invalid syntax
DValueError: The truth value of an array is ambiguous
Attempts:
2 left
💡 Hint
Check how chained comparisons work with numpy arrays.
🚀 Application
advanced
1:30remaining
Filter numpy array elements using comparison
Which option correctly filters elements greater than 10 from the numpy array?
NumPy
import numpy as np
arr = np.array([5, 12, 7, 18, 3])
filtered = ???
print(filtered)
Aarr.filter(arr > 10)
Barr > 10
Carr[arr > 10]
Dnp.filter(arr, arr > 10)
Attempts:
2 left
💡 Hint
Use boolean indexing to select elements.
🧠 Conceptual
expert
2:30remaining
Understanding broadcasting in numpy comparison
Given arr1 = np.array([1, 2, 3]) and arr2 = np.array([[2], [3], [4]]), what is the shape of the result of arr1 < arr2?
NumPy
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([[2], [3], [4]])
result = arr1 < arr2
print(result.shape)
A(1, 3)
B(3, 3)
C(3,)
D(3, 1)
Attempts:
2 left
💡 Hint
Consider how numpy broadcasts shapes (3,) and (3,1) for element-wise comparison.

Practice

(1/5)
1. What does the numpy expression arr == 5 return when arr is a numpy array?
easy
A. The sum of all elements equal to 5
B. An array of True/False values showing where elements equal 5
C. The count of elements equal to 5
D. A single True or False value

Solution

  1. Step 1: Understand element-wise comparison

    The expression arr == 5 compares each element of the array to 5 individually.
  2. Step 2: Result type of comparison

    This returns a new array of the same shape with True where the element equals 5, and False otherwise.
  3. Final Answer:

    An array of True/False values showing where elements equal 5 -> Option B
  4. Quick Check:

    Element-wise comparison = Boolean array [OK]
Hint: Remember == returns True/False array element-wise [OK]
Common Mistakes:
  • Thinking it returns a count or sum
  • Expecting a single True/False value
  • Confusing == with assignment =
2. Which of the following is the correct syntax to check if elements in numpy array arr are greater than 10?
easy
A. arr >> 10
B. arr => 10
C. arr > 10
D. arr >= 10

Solution

  1. Step 1: Identify correct comparison operator

    The operator for 'greater than' is >, so arr > 10 checks elements greater than 10.
  2. Step 2: Check other options

    arr => 10 is invalid syntax, arr >> 10 is a bitwise shift, and arr >= 10 checks greater or equal, not strictly greater.
  3. Final Answer:

    arr > 10 -> Option C
  4. Quick Check:

    Greater than operator is > [OK]
Hint: Use > for greater than, >= for greater or equal [OK]
Common Mistakes:
  • Using => instead of >
  • Confusing bitwise >> with comparison
  • Using >= when > is asked
3. What is the output of the following code?
import numpy as np
arr = np.array([3, 7, 2, 9])
result = arr >= 5
print(result)
medium
A. [False False False False]
B. [True False True False]
C. [True True True True]
D. [False True False True]

Solution

  1. Step 1: Compare each element with 5

    Elements: 3, 7, 2, 9. Check if each is >= 5: 3>=5 is False, 7>=5 is True, 2>=5 is False, 9>=5 is True.
  2. Step 2: Form boolean array

    The result is [False, True, False, True].
  3. Final Answer:

    [False True False True] -> Option D
  4. Quick Check:

    Element-wise >= 5 = [False True False True] [OK]
Hint: Check each element against 5 carefully [OK]
Common Mistakes:
  • Mixing up >= with >
  • Misreading element values
  • Expecting integer output instead of boolean
4. Identify the error in this numpy comparison code:
import numpy as np
arr = np.array([1, 2, 3])
result = arr = 2
print(result)
medium
A. Using single = instead of == for comparison
B. Array must be converted to list before comparison
C. print() cannot display numpy arrays
D. Missing parentheses in print statement

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]
Hint: Use == for comparison, = is assignment [OK]
Common Mistakes:
  • Confusing = and ==
  • Thinking print can't show arrays
  • Believing arrays must convert to lists
5. Given two numpy arrays:
import numpy as np
arr1 = np.array([1, 4, 6, 8])
arr2 = np.array([2, 4, 5, 10])

Which expression returns a boolean array showing where elements in arr1 are less than or equal to elements in arr2?
hard
A. arr1 <= arr2
B. arr1 < arr2
C. arr1 == arr2
D. arr1 >= arr2

Solution

  1. Step 1: Understand element-wise comparison between arrays

    Comparing two arrays with operators like <= compares each element pair at the same index.
  2. Step 2: Identify operator for 'less than or equal'

    The operator <= checks if elements in arr1 are less than or equal to corresponding elements in arr2.
  3. Final Answer:

    arr1 <= arr2 -> Option A
  4. Quick Check:

    Use <= for less or equal element-wise [OK]
Hint: Use <= to compare arrays element-wise for less or equal [OK]
Common Mistakes:
  • Using < instead of <= when equal counts
  • Using == which only checks equality
  • Using >= which is opposite
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard