Code Explanation:
1. Creating a NumPy array x
x = np.array([4, 5, 6])
Creates a NumPy array x with elements [4, 5, 6].
2. Assigning y to x
y = x
Assigns y to reference the same array object as x.
So, y and x point to the same data in memory.
3. Modifying an element of y
y[2] = 10
Changes the value at index 2 of y to 10.
Since x and y share the same data, this change also affects x.
Now both x and y are [4, 5, 10].
4. Creating a copy z of x
z = x.copy()
Creates a new independent copy of x and assigns it to z.
z now holds [4, 5, 10] initially but is a different object in memory.
5. Modifying an element of z
z[0] = 7
Changes the first element of z to 7.
Now, z is [7, 5, 10].
This does not affect x or y.
6. Calculating and printing the sum
print(np.sum(x) + np.sum(y) + np.sum(z))
Sum of x = 4 + 5 + 10 = 19
Sum of y = same as x = 19
Sum of z = 7 + 5 + 10 = 22
Total = 19 + 19 + 22 = 60
Output:
60
Download Book - 500 Days Python Coding Challenges with Explanation
.png)

0 Comments:
Post a Comment