0
0
NumPydata~15 mins

Indexing returns views not copies in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Indexing Returns Views Not Copies in NumPy
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(temps) to display the updated array.