Introduction
We combine conditions to select or filter data that meets multiple rules at the same time. This helps us find exactly what we want in big data sets.
Jump into concepts and practice - no test required
We combine conditions to select or filter data that meets multiple rules at the same time. This helps us find exactly what we want in big data sets.
combined_condition = (condition1) & (condition2) combined_condition = (condition1) | (condition2) combined_condition = ~(condition1)
Use & for AND, | for OR, and ~ for NOT.
Always put each condition inside parentheses to avoid errors.
import numpy as np arr = np.array([10, 20, 30, 40, 50]) condition = (arr > 20) & (arr < 50) print(arr[condition])
import numpy as np arr = np.array([10, 20, 30, 40, 50]) condition = (arr < 20) | (arr > 40) print(arr[condition])
import numpy as np arr = np.array([10, 20, 30, 40, 50]) condition = ~(arr == 30) print(arr[condition])
This program finds ages between 30 and 45 by combining two conditions with AND.
import numpy as np # Create an array of ages ages = np.array([18, 25, 30, 35, 40, 45, 50]) # Condition 1: Age greater than or equal to 30 cond1 = ages >= 30 # Condition 2: Age less than or equal to 45 cond2 = ages <= 45 # Combine conditions with AND to find ages between 30 and 45 inclusive combined = cond1 & cond2 # Print the filtered ages print(ages[combined])
Use parentheses around each condition to avoid mistakes.
Use & for AND, | for OR, and ~ for NOT when combining numpy conditions.
Combined conditions return a boolean array you can use to filter data.
Combine conditions with & (AND), | (OR), and ~ (NOT).
Always put each condition inside parentheses.
Use combined conditions to filter numpy arrays easily.
a > 5 and b < 10 in NumPy to select elements where both are true?& (AND) or | (OR) operators.a > 5 and b < 10 with AND is (a > 5) & (b < 10).(a > 5) & (b < 10) -> Option Aarr where values are NOT equal to 0 and less than 10?(arr != 0) and (arr < 10).& to combine conditions for selecting elements satisfying both.import numpy as np arr = np.array([1, 5, 8, 12, 3, 7]) result = arr[(arr > 3) | (arr == 1)] print(result)
arr > 3 is True for 5, 8, 12, 7; arr == 1 is True for 1.import numpy as np arr = np.array([2, 4, 6, 8]) filtered = arr[arr > 3 && arr < 8] print(filtered)
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?(data < 20) | (data >= 30) to select elements less than 20 or greater or equal to 30.& (data != 15) to exclude 15.