I have the following piece of code:
import numpy as np
arr = np.arange(10)
slice = arr[2:5]
slice[:] = 12
print(arr)
slice = slice / 2
print(arr)
The output will be:
[ 0 1 12 12 12 5 6 7 8 9]
[6. 6. 6.]
[ 0 1 12 12 12 5 6 7 8 9]
So the first time around slice is just a reference to part of arr so modifying it also changes the array, but the second time around it has become a copy of that part of the array and modifying it makes no difference in arr. Why does this happen? What makes slice = slice / 2 different?