📖 Scenario: Imagine you run a small bakery. You want to understand how many cakes you sell each day to plan better. You have sales numbers for a week and want to find the average, the smallest, and the biggest sales day.
🎯 Goal: You will use NumPy to calculate basic statistics like mean, minimum, and maximum from your sales data. This helps you see patterns and make smart decisions.
📋 What You'll Learn
Create a NumPy array with exact daily sales numbers
Create a variable to hold the sales data
Use NumPy functions to find mean, min, and max
Print the results clearly
💡 Why This Matters
🌍 Real World
Businesses use statistics to understand sales trends and plan inventory or marketing.
💼 Career
Data analysts and scientists use NumPy daily to analyze data quickly and accurately.
Progress0 / 4 steps
1
Create the sales data array
Import NumPy as np and create a NumPy array called sales with these exact daily sales numbers: 20, 35, 30, 35, 27, 25, 40.
NumPy
Hint
Use np.array() to create the array with the exact sales numbers.
2
Calculate the average sales
Create a variable called average_sales and use np.mean() on the sales array to find the average number of cakes sold.
NumPy
Hint
Use np.mean(sales) to get the average.
3
Find the minimum and maximum sales
Create two variables called min_sales and max_sales. Use np.min() on sales to find the smallest sales number and np.max() on sales to find the biggest sales number.
NumPy
Hint
Use np.min(sales) and np.max(sales) to find smallest and biggest sales.
4
Print the statistics
Print the values of average_sales, min_sales, and max_sales with clear messages. Use three separate print() statements.
NumPy
Hint
Use print(f"Average sales: {average_sales}") style to show results clearly.
Practice
(1/5)
1. Why is it useful to calculate statistics like mean and median using NumPy?
easy
A. Because it deletes unnecessary data points
B. Because it helps summarize large data sets quickly and clearly
C. Because it makes data harder to understand
D. Because it changes the original data values
Solution
Step 1: Understand the purpose of statistics
Statistics like mean and median give a simple summary of data, making it easier to understand large amounts of numbers.
Step 2: Recognize NumPy's role
NumPy provides fast and easy functions to calculate these statistics, saving time and effort.
Final Answer:
Because it helps summarize large data sets quickly and clearly -> Option B
Quick Check:
Statistics summarize data = Because it helps summarize large data sets quickly and clearly [OK]
Hint: Statistics simplify data understanding fast with NumPy [OK]
Common Mistakes:
Thinking statistics change original data
Believing statistics delete data points
Confusing summary with data removal
2. Which of the following is the correct way to calculate the mean of a NumPy array named data?
easy
A. np.mean(data)
B. np.mean[data]
C. data.mean()()
D. mean(data)
Solution
Step 1: Recall NumPy mean syntax
The correct function to calculate mean in NumPy is np.mean() with the array passed inside parentheses.
Step 2: Check each option
mean(data) misses the np. prefix, data.mean()() has extra parentheses, np.mean[data] uses square brackets incorrectly.
Final Answer:
np.mean(data) -> Option A
Quick Check:
Correct NumPy mean syntax = np.mean(data) [OK]
Hint: Use np.mean(array) to get mean quickly [OK]
Common Mistakes:
Forgetting to use np. prefix
Using square brackets instead of parentheses
Adding extra parentheses after method
3. What is the output of this code?
import numpy as np
data = np.array([1, 2, 3, 4, 5])
print(np.median(data))
medium
A. 2
B. 2.5
C. 3
D. 3.0
Solution
Step 1: Understand the median calculation
The median is the middle value when data is sorted. For [1,2,3,4,5], the middle is 3.
Step 2: Check NumPy median output type
NumPy returns median as a float, so output is 3.0, not integer 3.
Final Answer:
3.0 -> Option D
Quick Check:
Median of odd-length array = middle value as float [OK]
Hint: Median of odd list is middle number as float [OK]
Common Mistakes:
Confusing median with mean
Expecting integer instead of float output
Choosing average of two numbers for odd-length array
4. The following code is meant to calculate the standard deviation of data, but it gives an error. What is the problem?
import numpy as np
data = np.array([10, 20, 30])
std_dev = np.std data
print(std_dev)
medium
A. Data array is not numeric
B. np.std does not exist in NumPy
C. Missing parentheses after np.std
D. print() cannot display std_dev
Solution
Step 1: Identify syntax error in function call
The code calls np.std without parentheses, which is invalid syntax in Python.
Step 2: Correct the function call
Adding parentheses and passing data inside fixes the error: np.std(data).
Final Answer:
Missing parentheses after np.std -> Option C
Quick Check:
Function calls need parentheses [OK]
Hint: Always use parentheses when calling functions [OK]
Common Mistakes:
Forgetting parentheses on function calls
Thinking np.std is not a NumPy function
Assuming print cannot show numbers
5. You have a large dataset with some missing values represented as np.nan. Which NumPy function helps calculate the mean while ignoring these missing values?
hard
A. np.nanmean()
B. np.mean()
C. np.median()
D. np.std()
Solution
Step 1: Understand the problem with missing values
Missing values as np.nan cause normal mean calculations to return nan instead of a number.
Step 2: Use the correct NumPy function
np.nanmean() calculates the mean ignoring nan values, giving a valid average.
Final Answer:
np.nanmean() -> Option A
Quick Check:
Ignore NaNs with np.nanmean() [OK]
Hint: Use np.nanmean() to skip NaN values in mean [OK]
Common Mistakes:
Using np.mean() which returns nan if any NaN present
Confusing median with mean for missing data
Trying to use np.std() for mean calculation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.