0
0
NumPydata~15 mins

Why set operations matter in NumPy - See It in Action

Choose your learning style9 modes available
Why Set Operations Matter
📖 Scenario: Imagine you work in a store that sells fruits. You have two lists: one for fruits currently in stock and one for fruits customers want to buy. You want to find out which fruits are both in stock and wanted by customers, which fruits are only in stock, and which fruits are only wanted by customers.
🎯 Goal: You will use numpy set operations to find common fruits, fruits only in stock, and fruits only wanted by customers.
📋 What You'll Learn
Create two numpy arrays with exact fruit names
Create a variable to hold the common fruits
Use numpy set operations to find fruits only in stock and only wanted
Print the results clearly
💡 Why This Matters
🌍 Real World
Stores and businesses often need to compare lists of items, like stock vs customer demand, to make smart decisions.
💼 Career
Data analysts and scientists use set operations to clean data, find overlaps, and identify unique entries in datasets.
Progress0 / 4 steps
1
Create the fruit lists as numpy arrays
Create a numpy array called stock with these fruits: 'apple', 'banana', 'orange', 'kiwi', 'mango'.
NumPy
Need a hint?

Use np.array([...]) to create the array with the exact fruit names.

2
Create the customer wanted fruits array
Create a numpy array called wanted with these fruits: 'banana', 'kiwi', 'grape', 'mango', 'pineapple'.
NumPy
Need a hint?

Use np.array([...]) again to create the wanted array with the exact fruits.

3
Find common and unique fruits using numpy set operations
Create three variables: common for fruits in both stock and wanted using np.intersect1d(stock, wanted), only_in_stock for fruits only in stock using np.setdiff1d(stock, wanted), and only_wanted for fruits only in wanted using np.setdiff1d(wanted, stock).
NumPy
Need a hint?

Use np.intersect1d to find common fruits and np.setdiff1d to find fruits only in one array.

4
Print the results
Print the variables common, only_in_stock, and only_wanted each on a separate line with clear labels.
NumPy
Need a hint?

Use print("Label:", variable) to show each result clearly.