📖 Scenario: Imagine you are a data analyst working with sales data. You want to organize daily sales numbers into a structure that makes it easy to analyze and calculate totals.
🎯 Goal: You will create a NumPy array to hold sales data, set a threshold for high sales, filter the data based on this threshold, and finally display the filtered sales numbers.
📋 What You'll Learn
Create a NumPy array called sales with these exact values: 10, 25, 30, 5, 40
Create a variable called threshold and set it to 20
Use a NumPy expression to create a new array called high_sales that contains only sales values greater than threshold
Print the high_sales array
💡 Why This Matters
🌍 Real World
Data scientists often need to organize and filter data efficiently to find important information quickly.
💼 Career
Knowing how to create and filter arrays is a basic skill for data analysis and machine learning tasks.
Progress0 / 4 steps
1
Create the sales data array
Import NumPy as np and create a NumPy array called sales with these exact values: 10, 25, 30, 5, 40
NumPy
Hint
Use np.array([...]) to create the array.
2
Set the sales threshold
Create a variable called threshold and set it to the number 20
NumPy
Hint
Just assign the number 20 to threshold.
3
Filter sales above the threshold
Use a NumPy expression to create a new array called high_sales that contains only the values in sales greater than threshold
NumPy
Hint
Use boolean indexing: sales[sales > threshold].
4
Display the filtered sales
Print the high_sales array to show the sales numbers greater than the threshold
NumPy
Hint
Use print(high_sales) to display the array.
Practice
(1/5)
1. Why is it important to use np.array() to create arrays in NumPy instead of just using Python lists?
easy
A. Because NumPy arrays allow fast math operations on many numbers at once.
B. Because Python lists cannot store numbers.
C. Because np.array() creates strings, not numbers.
D. Because Python lists are faster than NumPy arrays.
Solution
Step 1: Understand Python lists vs NumPy arrays
Python lists store items but are slow for math on many numbers.
Step 2: Recognize NumPy arrays' advantage
NumPy arrays allow fast, efficient math on large sets of numbers.
Final Answer:
Because NumPy arrays allow fast math operations on many numbers at once. -> Option A
Quick Check:
NumPy arrays speed up math = A [OK]
Hint: NumPy arrays speed math on many numbers, lists do not [OK]
Common Mistakes:
Thinking Python lists can't store numbers
Confusing array creation with string creation
Believing lists are faster for math
2. Which of the following is the correct way to create a NumPy array from a Python list [1, 2, 3]?
easy
A. np.array{1, 2, 3}
B. array([1, 2, 3])
C. np.list([1, 2, 3])
D. np.array([1, 2, 3])
Solution
Step 1: Recall correct NumPy array syntax
The function to create arrays is np.array() with parentheses and a list inside.