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

NumPy · Function

np.tan

Computes tangent element-wise for array-like input, using radians. It returns corresponding tangent values and supports NumPy ufunc options such as output reuse and conditional computation.

How it works

np.tan applies the tangent function independently to each input value. Inputs are interpreted in radians, and array inputs are processed element by element with normal NumPy broadcasting behavior.

Syntax

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

Parameters

NameTypeDescription
xarray_likeArray-like values whose tangents are computed.
outndarray, None, or tuple of ndarray and NoneOptional destination array or tuple of destinations for the result.
wherearray_like of bool, optionalBoolean condition selecting where computation occurs.
casting{‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}, optionalCasting rule used when converting operands.
order{‘K’, ‘A’, ‘C’, ‘F’}, optionalMemory layout order for the output.
dtypedtype, optionalOutput data type.
subokbool, optionalWhether subclasses are allowed to pass through.

Returns

ndarray containing the corresponding tangent values; a scalar when x is scalar.

Key points

  • Calculates the tangent independently for every input element.
  • Mathematically, it corresponds element by element to sine divided by cosine.
  • Without an output destination, NumPy allocates and returns a new result array.
  • An out array must accommodate the broadcasted shape of the input.

Common mistakes

  • A scalar input produces a scalar result, while an array input produces an array of corresponding tangent values.
  • When using out, the destination is mutated and the returned object refers to that destination.
  • Values near angles where cosine is zero can become extremely large or be affected by floating-point behavior.

Example: Element-wise tangent

import numpy as np

angles = np.array([0.0, np.pi / 4, np.pi / 2])
result = np.tan(angles)
print(result)

Computes tangent independently for each angle in radians.

When to use it

Trigonometric calculations

Converting angular measurements into slope ratios.

Array-based numerical transforms

Applying tangent to a vector or matrix of angles.

Allocation-conscious computation

Writing results into reusable memory.

Interview questions

QWhat does np.tan compute?

np.tan is a NumPy ufunc that computes tangent element by element. Its input is interpreted in radians, and the result has the corresponding tangent values.

Follow-up: What happens for a scalar input? It returns a scalar rather than a one-element array.

QHow does the out parameter work?

The out argument supplies the destination array. It must be compatible with the broadcast shape of the inputs; when supplied, the function writes into it and returns a reference to it.

Follow-up: Why might out be useful? It can avoid allocating a new result array when reusing storage matters.

QWhich advanced controls does the signature expose?

The function accepts array-like input and supports optional where, casting, order, dtype, and subok controls inherited from the ufunc interface. The result is computed element-wise.

Follow-up: How would you restrict computation to selected elements? Pass a Boolean where mask and provide initialized output storage for positions not selected.

  • numpy.sin
  • numpy.cos
  • numpy.arctan
  • numpy.tanh

Source: numpy.tan — NumPy v2.3 Manual. Checked 2026-10-15.