Combine complex conditions using logical_and(), logical_or(), logical_not(), and & | ~ operators.
NumPy provides bitwise operators (`&`, `|`, `~`, `^`) and universal logical functions (`np.logical_and`, `np.logical_or`) to combine boolean conditions element-wise.
Combining conditions is like running data through two security checkpoints: `&` requires a pass from both gates; `|` lets anyone through who has either pass.
Use `&` for AND, `|` for OR, and `~` for NOT. Parentheses around each sub-expression are strictly required due to Python operator precedence.
mask = (arr > 10) & (arr < 40)mask = arr > 10 & arr < 40 # Python executes 10 & arr first!mask = (arr > 10) & (arr < 40)Without parentheses, `&` has higher precedence than `>` and evaluates `10 & arr` first.
Solve the objective below using NumPy vectorized syntax
Given `arr = np.array([2, 8, 14, 20, 26])`, filter numbers where `(arr > 5) & (arr < 25)` and print.
[ 8 14 20]