NumPy tan()

The numpy.tan() function computes the trigonometric tangent of each element in an input array. It is equivalent to np.sin(x) / np.cos(x) computed element-wise.

Syntax

</>
Copy
numpy.tan(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
xarray_likeInput array containing angles in radians.
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 tangent function.
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 tangent values of the input array elements. If the input is a scalar, a scalar is returned.


Examples

1. Computing Tangent of a Single Value

Here, we compute the tangent of a single angle in radians.

</>
Copy
import numpy as np

# Define an angle in radians
angle = np.pi / 4  # 45 degrees in radians

# Compute the tangent of the angle
result = np.tan(angle)

# Print the result
print("Tangent of 45 degrees (π/4 radians):", result)

Output:

Tangent of 45 degrees (π/4 radians): 0.9999999999999999