0
0
NumPydata~20 mins

Combining conditions in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Combining Conditions
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of combined conditions with numpy arrays
What is the output of this code snippet that combines two conditions on a numpy array?
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
result = arr[(arr > 2) & (arr < 5)]
print(result)
A[1 2 3 4 5]
B[2 3 4]
C[3 4]
D[4 5]
Attempts:
2 left
💡 Hint
Remember that & means 'and' and both conditions must be true for each element.
data_output
intermediate
2:00remaining
Number of elements matching combined conditions
How many elements in the array satisfy the combined condition (arr % 2 == 0) | (arr > 4)?
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
filtered = arr[(arr % 2 == 0) | (arr > 4)]
print(len(filtered))
A6
B4
C3
D5
Attempts:
2 left
💡 Hint
Check which numbers are even or greater than 4.
Predict Output
advanced
2:00remaining
Output of combined conditions with logical operators
What is the output of this code that uses combined conditions with numpy arrays?
NumPy
import numpy as np
arr = np.array([10, 15, 20, 25, 30])
result = arr[(arr >= 15) & ((arr % 10) == 0)]
print(result)
A[10 20 30]
B[15 20 25 30]
C[15 25]
D[20 30]
Attempts:
2 left
💡 Hint
Check elements greater or equal to 15 and divisible by 10.
🔧 Debug
advanced
2:00remaining
Identify the error in combining conditions with numpy arrays
What error does this code raise when combining conditions incorrectly?
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
result = arr[arr > 2 and arr < 4]
print(result)
AValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
BSyntaxError: invalid syntax
CTypeError: unsupported operand type(s) for &: 'bool' and 'bool'
DIndexError: index out of range
Attempts:
2 left
💡 Hint
Check how logical operators work with numpy arrays.
🚀 Application
expert
3:00remaining
Filter a numpy array with multiple combined conditions
Given the array arr = np.array([5, 10, 15, 20, 25, 30]), which option correctly filters elements that are either divisible by 5 but not by 10, or greater than 20?
NumPy
import numpy as np
arr = np.array([5, 10, 15, 20, 25, 30])
Aarr[((arr % 5 == 0) & (arr % 10 != 0)) | (arr > 20)]
Barr[(arr % 5 == 0) & ((arr % 10 != 0) | (arr > 20))]
Carr[(arr % 5 == 0) | (arr % 10 != 0) & (arr > 20)]
Darr[(arr % 5 == 0) & (arr % 10 != 0) | arr > 20]
Attempts:
2 left
💡 Hint
Use parentheses to group conditions properly and remember operator precedence.