Code Explanation:
Importing NumPy:
The statement import numpy as np imports the NumPy library, which is a popular Python library for numerical computations.
Creating an Array:
arr = np.array([1, 2, 3, 4]) creates a NumPy array with the elements [1, 2, 3, 4].
NumPy arrays are similar to Python lists but optimized for numerical operations.
Adding a Scalar to an Array:
arr + 2 adds the scalar value 2 to each element of the array. This operation is called broadcasting in NumPy.
Broadcasting automatically applies the operation to each element of the array without requiring a loop.
Printing the Result:
print(arr + 2) outputs the result of the broadcasting operation.
Final Output:
[3 4 5 6]
0 Comments:
Post a Comment