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

ParameterTypeDescription
x1, x2array_likeInput arrays to be added. They must have the same shape or be broadcastable.
outndarray, None, or tuple of ndarray and None, optionalOptional output array where the result is stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying which elements to compute. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing the addition.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines 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