NumPy add()
The numpy.add() function performs element-wise addition of two input arrays. If the arrays have different shapes, they must be broadcastable to a common shape.
Syntax
</>
Copy
numpy.add(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
| Parameter | Type | Description |
|---|---|---|
x1, x2 | array_like | Input arrays to be added. They must have the same shape or be broadcastable. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to compute. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when computing the addition. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns an array with the element-wise sum of x1 and x2. If both inputs are scalars, a scalar is returned.
Examples
1. Adding Two Scalars
Adding two scalar values using numpy.add().
</>
Copy
import numpy as np
# Define two scalar values
x1 = 10
x2 = 5
# Perform element-wise addition
result = np.add(x1, x2)
# Print the result
print("Sum of scalars:", result)
Output:
Sum of scalars: 15
