Challenge - 5 Problems
Master of Combining Conditions
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember that & means 'and' and both conditions must be true for each element.
✗ Incorrect
The code selects elements greater than 2 and less than 5, which are 3 and 4.
❓ data_output
intermediate2: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))
Attempts:
2 left
💡 Hint
Check which numbers are even or greater than 4.
✗ Incorrect
Elements 2, 4, 5, 6 satisfy the condition.
❓ Predict Output
advanced2: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)
Attempts:
2 left
💡 Hint
Check elements greater or equal to 15 and divisible by 10.
✗ Incorrect
Only 20 and 30 satisfy both conditions.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Check how logical operators work with numpy arrays.
✗ Incorrect
Using 'and' with numpy arrays causes a ValueError because it expects a single boolean, not an array.
🚀 Application
expert3: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])
Attempts:
2 left
💡 Hint
Use parentheses to group conditions properly and remember operator precedence.
✗ Incorrect
Option A correctly groups conditions to select elements divisible by 5 but not 10, or greater than 20.