Bird
Raised Fist0
NumPydata~20 mins

Combining conditions in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. Which of the following is the correct way to combine two conditions a > 5 and b < 10 in NumPy to select elements where both are true?
easy
A. Use (a > 5) & (b < 10)
B. Use a > 5 & b < 10 without parentheses
C. Use (a > 5) | (b < 10)
D. Use a > 5 or b < 10

Solution

  1. Step 1: Understand combining conditions in NumPy

    NumPy requires each condition to be in parentheses when using & (AND) or | (OR) operators.
  2. Step 2: Identify correct syntax for AND condition

    The correct way to combine a > 5 and b < 10 with AND is (a > 5) & (b < 10).
  3. Final Answer:

    Use (a > 5) & (b < 10) -> Option A
  4. Quick Check:

    Parentheses + & = correct AND condition [OK]
Hint: Always put each condition in parentheses when combining [OK]
Common Mistakes:
  • Omitting parentheses around conditions
  • Using Python 'and' instead of '&' for arrays
  • Using 'or' instead of '|' for arrays
2. Which of the following is the correct syntax to select elements from a NumPy array arr where values are NOT equal to 0 and less than 10?
easy
A. arr[arr != 0 & arr < 10]
B. arr[(arr != 0) & (arr < 10)]
C. arr[(arr != 0) | (arr < 10)]
D. arr[~arr != 0 & arr < 10]

Solution

  1. Step 1: Use parentheses for each condition

    Each condition must be enclosed in parentheses: (arr != 0) and (arr < 10).
  2. Step 2: Combine with AND operator

    Use & to combine conditions for selecting elements satisfying both.
  3. Final Answer:

    arr[(arr != 0) & (arr < 10)] -> Option B
  4. Quick Check:

    Parentheses + & + correct conditions = syntax correct [OK]
Hint: Use parentheses around each condition and '&' for AND [OK]
Common Mistakes:
  • Missing parentheses causing syntax errors
  • Using bitwise NOT (~) incorrectly
  • Using Python 'and' instead of '&'
3. Given the code:
import numpy as np
arr = np.array([1, 5, 8, 12, 3, 7])
result = arr[(arr > 3) | (arr == 1)]
print(result)

What is the output?
medium
A. [5 8 12 7]
B. [1 5 8 12 3 7]
C. [5 8 12]
D. [1 5 8 12 7]

Solution

  1. Step 1: Evaluate each condition on the array

    arr > 3 is True for 5, 8, 12, 7; arr == 1 is True for 1.
  2. Step 2: Combine conditions with OR operator

    Elements where either condition is True are selected: 1, 5, 8, 12, 7.
  3. Final Answer:

    [1 5 8 12 7] -> Option D
  4. Quick Check:

    OR condition selects 1 and all >3 values [OK]
Hint: OR (|) selects elements matching either condition [OK]
Common Mistakes:
  • Forgetting to include elements equal to 1
  • Using AND (&) instead of OR (|)
  • Misreading the array values
4. What is wrong with this code snippet?
import numpy as np
arr = np.array([2, 4, 6, 8])
filtered = arr[arr > 3 && arr < 8]
print(filtered)
medium
A. Missing parentheses around conditions
B. Using Python 'and' instead of '&'
C. Using '&&' instead of '&' for combining conditions
D. No error, code runs fine

Solution

  1. Step 1: Identify operator error

    NumPy uses bitwise operators '&' and '|' for element-wise logical operations, not '&&'.
  2. Step 2: Correct operator usage

    Replace '&&' with '&' and add parentheses around each condition.
  3. Final Answer:

    Using '&&' instead of '&' for combining conditions -> Option C
  4. Quick Check:

    '&&' is invalid in NumPy, use '&' with parentheses [OK]
Hint: Use '&' not '&&' for NumPy condition combining [OK]
Common Mistakes:
  • Using '&&' from other languages
  • Not adding parentheses around conditions
  • Using Python 'and' instead of '&'
5. You have a NumPy array data = np.array([10, 15, 20, 25, 30, 35]). You want to select elements that are either less than 20 or greater than or equal to 30, but NOT equal to 15. Which code correctly filters data?
hard
A. data[((data < 20) | (data >= 30)) & (data != 15)]
B. data[(data < 20) | (data >= 30) & (data != 15)]
C. data[(data < 20) & (data >= 30) & (data != 15)]
D. data[~((data < 20) | (data >= 30) & (data == 15))]

Solution

  1. Step 1: Combine OR conditions inside parentheses

    Use (data < 20) | (data >= 30) to select elements less than 20 or greater or equal to 30.
  2. Step 2: Exclude elements equal to 15 with AND

    Combine with & (data != 15) to exclude 15.
  3. Final Answer:

    data[((data < 20) | (data >= 30)) & (data != 15)] -> Option A
  4. Quick Check:

    Parentheses + OR + AND + NOT = correct filter [OK]
Hint: Group OR conditions, then AND with NOT condition [OK]
Common Mistakes:
  • Missing parentheses causing wrong precedence
  • Using AND instead of OR for first condition
  • Incorrect negation of 15