Step-by-step Explanation
-
Create NumPy array
a is a NumPy array stored in memory.
-
Make a copy
.copy() creates a new array in a different memory location
a and b are now independent
-
Modify the copied array
-
Changes only b
a remains unchanged
-
Print original array
✅ Output
[5 6 7]Key Concept (Exam & Interview Favorite)
| Method | Memory Shared? | Change affects original? |
|---|---|---|
| = | ✅ Yes | ✅ Yes |
| .view() | ✅ Yes | ✅ Yes |
| .copy() | ❌ No | ❌ No |
One-line Takeaway
.copy() creates a completely independent NumPy array.
900 Days Python Coding Challenges with Explanation


0 Comments:
Post a Comment