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
numpy.tan(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x | array_like | Input array containing angles in radians. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to compute. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when computing the tangent function. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines 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.
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

2. Computing Tangent for an Array of Angles
We compute the tangent values for multiple angles provided in an array.
import numpy as np
# Define an array of angles in radians
angles = np.array([0, np.pi/6, np.pi/4, np.pi/3, np.pi/2]) # [0°, 30°, 45°, 60°, 90°]
# Compute the tangent of each angle
tan_values = np.tan(angles)
# Print the results
print("Angles (in radians):", angles)
print("Tangent values:", tan_values)
Output:
Angles (in radians): [0. 0.52359878 0.78539816 1.04719755 1.57079633]
Tangent values: [0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00
1.63312394e+16]

Note that the tangent function approaches infinity at π/2
(90°), which is why the last value is very large.
3. Using the out
Parameter
Using an output array to store results instead of creating a new array.
import numpy as np
# Define an array of angles
angles = np.array([0, np.pi/4, np.pi/2, np.pi])
# Create an output array with the same shape
output_array = np.empty_like(angles)
# Compute tangent and store the result in output_array
np.tan(angles, out=output_array)
# Print the results
print("Computed tangent values:", output_array)
Output:
Computed tangent values: [ 0.00000000e+00 1.00000000e+00 1.63312394e+16 -1.22464680e-16]

4. Using the where
Parameter
Using a condition to compute tangent only for selected elements.
import numpy as np
# Define an array of angles
angles = np.array([0, np.pi/4, np.pi/2, np.pi])
# Define a mask (compute tangent only where mask is True)
mask = np.array([True, False, True, False])
# Compute tangent values where mask is True
result = np.tan(angles, where=mask)
# Print the results
print("Computed tangent values with mask:", result)
Output:
Computed tangent values with mask: [0.00000000e+00 0.00000000e+00 1.63312394e+16 0.00000000e+00]

The tangent values are computed only for elements where mask=True
. The other values remain unchanged.