Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.add

Adds two inputs element by element. Compatible shapes are broadcast, and the result is an array or scalar depending on the inputs.

How it works

np.add is a universal function that computes the element-wise sum of two inputs. NumPy broadcasts compatible shapes, applies the selected type and output rules, and returns either an array or a scalar.

Syntax

numpy.add(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True [, signature])

Parameters

NameTypeDescription
x1array_likeThe first array-like operand.
x2array_likeThe second array-like operand.
outndarray or NoneA location into which the result is written.
wherearray_like of bool or boolBoolean condition selecting output positions.
casting{'no','equiv','safe','same_kind','unsafe'}, optionalControls permitted input-to-output casting.
order{'K','A','C','F'}, optionalMemory layout order for the result.
dtypedtype, optionalOutput data type.
subokbool, optionalWhether subclasses are propagated.
signaturestring or tuple of ints, optionalGeneralized-ufunc signature when applicable.

Returns

add: ndarray or scalar — the element-wise sum; a scalar when both inputs are scalars.

Key points

  • It computes addition independently at each corresponding output position.
  • For broadcasting behavior, it is equivalent to the array addition operator.
  • x1 and x2 are the two array-like operands.
  • Its returned values are the element-wise sums of the operands.

Common mistakes

  • Inputs with different shapes must be broadcast-compatible; otherwise the operation fails.
  • Scalar inputs produce a scalar result rather than a zero-dimensional array.

Example: Broadcasted element-wise addition

import numpy as np

values = np.array([[1, 2, 3], [4, 5, 6]])
offset = np.array([10, 20, 30])
result = np.add(values, offset)
print(result)

Adds the one-dimensional offset to each row through broadcasting.

When to use it

Array arithmetic

Combine vectors, matrices, or other numeric arrays position by position.

Broadcasted offsets

Add a shared row- or column-shaped offset to a larger array.

Destination-buffer computation

Write sums into an existing buffer when controlling allocation matters.

Interview questions

QWhat does np.add do?

np.add is a ufunc that adds two inputs element by element. It supports broadcasting, so compatible unequal shapes are expanded conceptually to a common shape.

Follow-up: How would you add an array to every row of a matrix? Pass a one-dimensional array whose length matches the matrix's column dimension; broadcasting aligns it with each row.

QWhat does np.add return?

The result is an ndarray for array inputs or a scalar when both inputs are scalars. Its values are the element-wise sums of the two operands.

Follow-up: What determines the array's shape? Broadcasting determines a common output shape from the input shapes.

QWhich optional controls are available?

out can receive the computed result, while where controls which positions are evaluated or assigned. The function also exposes casting, ordering, dtype, and subclass-related options.

Follow-up: Why use out? It permits writing into a supplied output array, which can avoid allocating another result when the supplied array is suitable.

  • numpy.ufunc
  • numpy.add.at
  • numpy.add.reduce
  • numpy.add.accumulate
  • numpy.add.reduceat

Source: numpy.add. Checked 2026-10-15.