📖 Scenario: Imagine you have a list of daily temperatures for a week stored in a NumPy array. You want to look at just the middle three days and see how changing those values affects the original data.
🎯 Goal: You will create a NumPy array of temperatures, select a slice of it, modify the slice, and observe how the original array changes because indexing returns a view, not a copy.
📋 What You'll Learn
Create a NumPy array called temps with these exact values: [20, 22, 19, 23, 21, 18, 20]
Create a variable called mid_week that selects the middle three days from temps using slicing
Change the first value in mid_week to 25
Print the temps array to show the change
💡 Why This Matters
🌍 Real World
Understanding views vs copies helps when working with large datasets to avoid unexpected changes and save memory.
💼 Career
Data scientists often manipulate data slices; knowing when changes affect original data prevents bugs and improves efficiency.
Progress0 / 4 steps
1
Create the temperatures array
Create a NumPy array called temps with these exact values: [20, 22, 19, 23, 21, 18, 20]
NumPy
Hint
Use np.array() to create the array with the exact values inside the brackets.
2
Select the middle three days
Create a variable called mid_week that selects the middle three days from temps using slicing with temps[2:5]
NumPy
Hint
Use slicing with temps[2:5] to get the middle three days (index 2, 3, and 4).
3
Modify the slice
Change the first value in mid_week to 25 by assigning mid_week[0] = 25
NumPy
Hint
Assign 25 to the first element of mid_week using index 0.
4
Print the original array
Print the temps array using print(temps) to show how it changed after modifying mid_week
NumPy
Hint
Use print(temps) to display the updated array.
Practice
(1/5)
1. What happens when you create a slice of a NumPy array using indexing like arr[1:4]?
easy
A. It returns a view that shares data with the original array.
B. It returns a completely independent copy of the array slice.
C. It returns a list instead of a NumPy array.
D. It raises an error because slicing is not allowed.
Solution
Step 1: Understand NumPy slicing behavior
In NumPy, slicing an array returns a view, not a copy. This means the new array shares the same data buffer as the original.
Step 2: Consequence of view sharing data
Because the slice is a view, changes to it will affect the original array, confirming it is not an independent copy.
Final Answer:
It returns a view that shares data with the original array. -> Option A
Quick Check:
Indexing slice = view sharing data [OK]
Hint: Slicing returns views, not copies, by default [OK]
Common Mistakes:
Thinking slicing creates a copy
Confusing views with copies
Assuming slicing returns a list
2. Which of the following is the correct way to create a copy of a NumPy array slice so that changes do NOT affect the original array?
easy
A. slice = arr[2:5]
B. slice = arr.copy[2:5]
C. slice = arr[2:5].copy()
D. slice = arr[2:5].view()
Solution
Step 1: Identify method to create independent copy
The .copy() method creates a new array with its own data, independent of the original.
Step 2: Check syntax correctness
Only arr[2:5].copy() correctly calls copy() on the slice. Other options either miss parentheses or use wrong methods.
Final Answer:
slice = arr[2:5].copy() -> Option C
Quick Check:
Use .copy() for independent array [OK]
Hint: Use .copy() after slicing to avoid shared data [OK]
B. Because slice_arr + 10 creates a new array, not modifying the view.
C. Because arr is immutable.
D. Because slicing always creates a copy, not a view.
Solution
Step 1: Analyze the operation slice_arr + 10
This operation creates a new array with values increased by 10. It does not modify slice_arr in place.
Step 2: Understand assignment effect
Assigning slice_arr = slice_arr + 10 makes slice_arr point to a new array. The original arr remains unchanged because no in-place modification happened.
Final Answer:
Because slice_arr + 10 creates a new array, not modifying the view. -> Option B
Quick Check:
slice + scalar creates new array, no original change [OK]
Hint: Adding to slice creates new array, original stays same [OK]
Common Mistakes:
Assuming slice_arr + 10 modifies original array
Thinking slicing always copies
Believing arrays are immutable
5. You have a large NumPy array data and want to modify a slice without affecting the original array. Which approach is best to ensure safety and memory efficiency?
hard
A. Use slice = data[100:200] and modify slice directly.
B. Modify data directly without slicing.
C. Use slice = data[100:200].view() and modify slice.
D. Use slice = data[100:200].copy() to modify safely without changing data.
Solution
Step 1: Understand views vs copies for safety
Modifying a view changes the original array. To avoid this, a copy is needed.
Step 2: Consider memory efficiency
Copying only the needed slice with .copy() uses less memory than copying the whole array and ensures safe modification.
Step 3: Evaluate other options
Using slice = data[100:200] and modifying slice directly modifies the original data unintentionally. Using slice = data[100:200].view() and modifying slice creates a view, which is not safe. Modifying data directly without slicing affects the entire array, not just the slice.
Final Answer:
Use slice = data[100:200].copy() to modify safely without changing data. -> Option D
Quick Check:
Copy slice for safe, efficient modification [OK]
Hint: Copy slice to avoid changing original large array [OK]
Common Mistakes:
Modifying views thinking they are copies
Copying entire array unnecessarily
Ignoring memory use in large arrays
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.