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

NumPy · Function

np.sqrt

Computes the non-negative square root element-wise for array-like input. Real negative values produce nan, while complex input produces complex square roots.

How it works

np.sqrt is a universal function that applies the non-negative square-root operation independently to each element of array-like input. It returns an array with the input shape, while its optional ufunc controls can direct storage, masking, casting, memory order, output type, and subclass behavior.

Syntax

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

Parameters

NameTypeDescription
xarray_likeValues whose square-roots are required.
outndarray, None, or tuple of ndarray and None, optionalDestination for the result; may be an ndarray, None, or a tuple of ndarray and None.
wherearray_like of bool, optionalBoolean or array-like mask selecting elements to compute.
casting{‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}, optionalCasting rule for the ufunc operation.
order{‘K’, ‘A’, ‘C’, ‘F’}, optionalMemory layout order of the result.
dtypedtype, optionalOutput data type.
subokbool, optionalWhether to pass through subclasses of ndarray.
signature{None, str, tuple of ints}, optionalGeneralized ufunc signature, when applicable.

Returns

y : ndarray — An array with the same shape as x containing the positive square-root of each element. Complex input yields a complex array; real negative elements yield nan.

Key points

  • Applies the non-negative square-root operation independently to every element.
  • Accepts x as array-like input, so lists and arrays can be passed directly.
  • Returns an array preserving the input shape, with each element replaced by its positive square root.
  • The optional out parameter lets callers specify destination storage for the result.
  • Complex inputs support square roots of negative real values instead of returning real-domain nan.

Common mistakes

  • Real negative inputs produce nan rather than complex roots; convert the input to a complex dtype when complex results are intended.
  • A single complex element makes the result complex, and negative real values in that complex computation receive complex square roots.

Example: Element-wise square roots

import numpy as np

values = np.array([4.0, 9.0, 16.0])
roots = np.sqrt(values)
print(roots)  # [2. 3. 4.]

Computes the square root of each array element and prints the resulting array.

When to use it

Calculate element-wise roots

Transforming numeric arrays

Handle negative radicands with complex arithmetic

Working with complex-valued data

Write results into provided output storage

Memory-conscious numerical pipelines

Apply roots under a boolean mask

Conditional array computation

Interview questions

QWhat does np.sqrt return for real and complex inputs?

np.sqrt is a ufunc that returns the non-negative square root element-wise. For real input, negative values produce nan; complex input produces complex results.

Follow-up: What happens if an input array mixes real and complex values? The result is a complex array because the presence of any complex element causes complex output.

QHow does output shape and storage work?

The result has the same shape as x. The out argument can provide an array or tuple of arrays where NumPy stores the computed values.

Follow-up: Why use out? It lets callers reuse storage and avoid allocating a separate result array when the destination is suitable.

QWhich keyword options control computation?

where controls which elements are evaluated, while dtype, casting, order, and subok control aspects of ufunc execution and the returned or stored result. The documented signature exposes these as keyword-only options.

Follow-up: What must be handled when using where without out? Unselected positions in a newly allocated result may be uninitialized, so initialize the result if those values will be read.

  • numpy.emath.sqrt
  • numpy.square
  • numpy.power

Source: numpy.sqrt — NumPy v2.5 Manual. Checked 2026-10-15.