📖 Scenario: Imagine you work in a grocery store. You have a list of prices for different fruits. You want to quickly find out the total cost if you buy one of each fruit. Doing this one by one is slow. Using arrays helps you do this fast and easily.
🎯 Goal: You will create a list of fruit prices, convert it to a NumPy array, and then calculate the total cost using array processing. This shows why array processing matters for fast and simple calculations.
📋 What You'll Learn
Create a list of fruit prices
Convert the list to a NumPy array
Calculate the total cost using array sum
Print the total cost
💡 Why This Matters
🌍 Real World
Stores and businesses often need to quickly calculate totals for many items. Using arrays helps them do this efficiently.
💼 Career
Data scientists and analysts use array processing to handle large datasets and perform fast calculations in many industries.
Progress0 / 4 steps
1
Create a list of fruit prices
Create a list called fruit_prices with these exact values: 1.2, 0.5, 2.0, 1.5, 0.8
NumPy
Hint
Use square brackets to create a list and separate values with commas.
2
Convert the list to a NumPy array
Import NumPy as np and create a NumPy array called prices_array from the list fruit_prices
NumPy
Hint
Use np.array() to convert a list to a NumPy array.
3
Calculate the total cost using array sum
Use the NumPy array prices_array and create a variable called total_cost that stores the sum of all prices using np.sum()
NumPy
Hint
Use np.sum() to add all elements in the array.
4
Print the total cost
Print the variable total_cost to display the total price of all fruits
NumPy
Hint
Use print(total_cost) to show the result.
Practice
(1/5)
1. Why is array processing important in data science?
easy
A. It makes code longer and more complex.
B. It slows down calculations significantly.
C. It allows handling many numbers at once efficiently.
D. It only works with text data.
Solution
Step 1: Understand array processing purpose
Array processing helps to work with many numbers together instead of one by one.
Step 2: Identify the benefit in data science
This makes calculations faster and code simpler, which is very useful in data science.
Final Answer:
It allows handling many numbers at once efficiently. -> Option C
Quick Check:
Array processing = efficient handling [OK]
Hint: Think: many numbers, one operation, faster code [OK]
Common Mistakes:
Thinking array processing slows code
Believing it only works with text
Assuming it makes code complex
2. Which of the following is the correct way to create a NumPy array from a list [1, 2, 3]?
easy
A. np.array([1, 2, 3])
B. np.list([1, 2, 3])
C. array([1, 2, 3])
D. np.create_array(1, 2, 3)
Solution
Step 1: Recall NumPy array creation syntax
NumPy arrays are created using np.array() with a list inside parentheses.
Step 2: Check each option
Only np.array([1, 2, 3]) matches the correct syntax; others are invalid or undefined.
Final Answer:
np.array([1, 2, 3]) -> Option A
Quick Check:
Use np.array() to create arrays [OK]
Hint: Remember: np.array() creates arrays from lists [OK]
Common Mistakes:
Using np.list() which does not exist
Omitting np prefix
Using wrong function names
3. What is the output of this code?
import numpy as np
arr = np.array([1, 2, 3])
result = arr * 2
print(result)
medium
A. [2 4 6]
B. [1 2 3 1 2 3]
C. [3 4 5]
D. Error: unsupported operation
Solution
Step 1: Understand array multiplication
Multiplying a NumPy array by a number multiplies each element by that number.
Step 2: Calculate element-wise multiplication
Each element in [1, 2, 3] multiplied by 2 gives [2, 4, 6].
Final Answer:
[2 4 6] -> Option A
Quick Check:
Array * number = element-wise multiply [OK]
Hint: Array * number multiplies each element [OK]
Common Mistakes:
Thinking it concatenates arrays
Expecting addition instead of multiplication
Assuming error due to operator
4. Find the error in this code snippet:
import numpy as np
arr = np.array([1, 2, 3])
result = arr + '2'
print(result)
medium
A. Missing import statement for NumPy.
B. Cannot add string to NumPy array of numbers.
C. Array should be created with np.list().
D. The print statement syntax is wrong.
Solution
Step 1: Identify data types involved in addition
The array contains numbers, but the code tries to add a string '2'.
Step 2: Understand type compatibility in NumPy
NumPy does not support adding strings to numeric arrays directly, causing a TypeError.
Final Answer:
Cannot add string to NumPy array of numbers. -> Option B
Quick Check:
Numeric array + string = error [OK]
Hint: Check data types before adding arrays [OK]
Common Mistakes:
Ignoring type mismatch
Thinking np.list() is valid
Blaming print syntax
5. You have two NumPy arrays:
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
You want to create a new array that contains the sum of corresponding elements from a and b. Which code correctly does this efficiently?
hard
A. c = a + b
B. c = [a[i] + b[i] for i in range(len(a))]
C. c = np.add(a, b)
D. Both A and D
Solution
Step 1: Understand element-wise addition in NumPy
NumPy supports element-wise addition using both the + operator and the np.add() function.
Step 2: Compare options for efficiency
Both c = a + b and c = np.add(a, b) perform fast, vectorized addition without explicit loops.
Step 3: Identify less efficient option
c = [a[i] + b[i] for i in range(len(a))] uses a Python loop, which is slower and less efficient than vectorized operations.
Final Answer:
Both A and D -> Option D
Quick Check:
Vectorized addition = a + b or np.add(a, b) [OK]
Hint: Use vectorized ops: + or np.add() for arrays [OK]
Common Mistakes:
Using slow Python loops instead of vectorized ops
Thinking only one method works
Ignoring NumPy's built-in functions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.