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
Filtering Data with Combined Conditions using NumPy
📖 Scenario: You work in a store and have a list of product prices. You want to find which products are both affordable and on sale.
🎯 Goal: Learn how to use NumPy to filter data by combining two conditions.
📋 What You'll Learn
Create a NumPy array with given prices
Create a price limit variable
Use combined conditions with NumPy to find affordable and on-sale products
Print the filtered prices
💡 Why This Matters
🌍 Real World
Filtering data with multiple conditions is common in sales, finance, and many other fields to find items that meet several criteria.
💼 Career
Data scientists and analysts often combine conditions to clean and analyze data efficiently using tools like NumPy.
Progress0 / 4 steps
1
Create the prices array
Create a NumPy array called prices with these exact values: 10, 25, 30, 45, 50, 60.
NumPy
Hint
Use np.array([...]) to create the array with the given numbers.
2
Set the price limit
Create a variable called price_limit and set it to 40.
NumPy
Hint
Just assign the number 40 to the variable price_limit.
3
Filter prices with combined conditions
Create a variable called affordable_and_on_sale that selects prices from prices which are less than price_limit and also less than 50. Use combined conditions with & and parentheses.
NumPy
Hint
Use parentheses around each condition and combine them with & inside the brackets.
4
Print the filtered prices
Print the variable affordable_and_on_sale to see the filtered prices.
NumPy
Hint
Use print(affordable_and_on_sale) to show the result.
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
Step 1: Understand combining conditions in NumPy
NumPy requires each condition to be in parentheses when using & (AND) or | (OR) operators.
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).
Final Answer:
Use (a > 5) & (b < 10) -> Option A
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
Step 1: Use parentheses for each condition
Each condition must be enclosed in parentheses: (arr != 0) and (arr < 10).
Step 2: Combine with AND operator
Use & to combine conditions for selecting elements satisfying both.
C. Using '&&' instead of '&' for combining conditions
D. No error, code runs fine
Solution
Step 1: Identify operator error
NumPy uses bitwise operators '&' and '|' for element-wise logical operations, not '&&'.
Step 2: Correct operator usage
Replace '&&' with '&' and add parentheses around each condition.
Final Answer:
Using '&&' instead of '&' for combining conditions -> Option C
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?