📖 Scenario: You work in a small bakery that tracks daily sales of different bread types. The sales data is recorded as Python lists for each day.You want to use NumPy to analyze this data more easily.
🎯 Goal: Create a NumPy array from the given Python lists of daily bread sales.
📋 What You'll Learn
Use the np.array() function to convert Python lists into a NumPy array.
Create the array exactly from the provided lists.
Print the resulting NumPy array.
💡 Why This Matters
🌍 Real World
In many jobs, data comes as lists or tables. Converting these into NumPy arrays helps with fast calculations and analysis.
💼 Career
Data scientists and analysts often convert raw data into arrays to prepare for statistical analysis, machine learning, or visualization.
Progress0 / 4 steps
1
Create Python lists for daily sales
Create two Python lists called day1_sales and day2_sales with these exact values: day1_sales = [10, 15, 7] and day2_sales = [12, 18, 9].
NumPy
Hint
Use square brackets [] to create lists with the exact numbers.
2
Import NumPy library
Import the NumPy library using the standard alias np.
NumPy
Hint
Use import numpy as np to import the library.
3
Create a NumPy array from the lists
Use np.array() to create a NumPy array called sales_array from the two lists day1_sales and day2_sales combined inside another list.
NumPy
Hint
Pass a list containing day1_sales and day2_sales to np.array().
4
Print the NumPy array
Print the variable sales_array to display the NumPy array of sales data.
NumPy
Hint
Use print(sales_array) to show the array.
Practice
(1/5)
1. What does np.array() do when given a Python list?
easy
A. It converts the list into a numpy array for easier math operations.
B. It changes the list into a Python dictionary.
C. It sorts the list in ascending order.
D. It removes duplicates from the list.
Solution
Step 1: Understand the purpose of np.array()
np.array() is designed to convert Python lists into numpy arrays.
Step 2: Recognize the benefit of numpy arrays
Numpy arrays allow fast, element-wise math operations that lists do not support.
Final Answer:
It converts the list into a numpy array for easier math operations. -> Option A
Quick Check:
np.array() converts lists to arrays [OK]
Hint: np.array() turns lists into arrays for math [OK]
Common Mistakes:
Thinking np.array() sorts the list
Confusing arrays with dictionaries
Assuming it removes duplicates
2. Which of the following is the correct syntax to create a numpy array from a Python list [1, 2, 3]?
easy
A. np.array{1, 2, 3}
B. np.array[[1, 2, 3]]
C. np.array(1, 2, 3)
D. np.array([1, 2, 3])
Solution
Step 1: Recall correct function call syntax
np.array() requires parentheses and a list inside, like np.array([1, 2, 3]).
Step 2: Identify syntax errors in other options
np.array{1, 2, 3} uses curly braces which is invalid; C passes multiple arguments instead of one list; D uses double brackets incorrectly.
Final Answer:
np.array([1, 2, 3]) -> Option D
Quick Check:
np.array() needs a list inside parentheses [OK]
Hint: Use parentheses and a list inside for np.array() [OK]
Common Mistakes:
Using curly braces instead of parentheses
Passing multiple arguments instead of a single list