Code Explanation:
1. Importing NumPy
import numpy as np
Loads the NumPy library.
Gives it the alias np so we can write np.function() instead of numpy.function().
2. Creating an Array Using arange
arr = np.arange(5) * 3
np.arange(5) creates:
[0, 1, 2, 3, 4]
Multiplying by 3 scales every element:
[0, 3, 6, 9, 12]
Now:
arr = [0, 3, 6, 9, 12]
3. Modifying Elements Greater Than 6
arr[arr > 6] = arr[arr > 6] - 2
arr > 6 creates a Boolean mask:
[False, False, False, True, True]
arr[arr > 6] selects the values [9, 12].
Subtracting 2 from them → [7, 10].
These new values replace the originals in arr.
Now:
arr = [0, 3, 6, 7, 10]
4. Calculating the Mean
print(arr.mean())
Mean = sum of elements ÷ number of elements.
Sum: 0 + 3 + 6 + 7 + 10 = 26
Count: 5
Mean = 26 / 5 = 5.2
Output:
5.2
Final Output:
5.2
Download Book - 500 Days Python Coding Challenges with Explanation
.png)

0 Comments:
Post a Comment