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 slicingChange the first value in
mid_week to 25Print 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