NumPy deg2rad()
The numpy.deg2rad()
function converts angles from degrees to radians. This is useful when working with trigonometric functions, which typically require angles in radians.
Syntax
</>
Copy
numpy.deg2rad(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x | array_like | Angles in degrees to be converted into 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 convert. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when converting degrees to radians. |
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 converted angles in radians. If the input is a scalar, a scalar is returned.
Examples
1. Converting a Single Angle from Degrees to Radians
Here, we convert 180 degrees to radians.
</>
Copy
import numpy as np
# Define an angle in degrees
angle_deg = 180
# Convert the angle to radians
angle_rad = np.deg2rad(angle_deg)
# Print the result
print("180 degrees in radians:", angle_rad)
Output:
180 degrees in radians: 3.141592653589793

2. Converting an Array of Angles
We convert an array of degree values to radians.
</>
Copy
import numpy as np
# Define an array of angles in degrees
angles_deg = np.array([0, 30, 45, 60, 90, 180, 270, 360])
# Convert the angles to radians
angles_rad = np.deg2rad(angles_deg)
# Print the results
print("Angles in degrees:", angles_deg)
print("Converted angles in radians:", angles_rad)
Output:
Angles in degrees: [ 0 30 45 60 90 180 270 360]
Converted angles in radians: [0. 0.52359878 0.78539816 1.04719755 1.57079633 3.14159265
4.71238898 6.28318531]

3. Using the out
Parameter
Using an output array to store the converted values.
</>
Copy
import numpy as np
# Define an array of angles
angles_deg = np.array([0, 45, 90, 180])
# Create an output array with the same shape
output_array = np.empty_like(angles_deg, dtype=np.float64)
# Convert degrees to radians and store the result in output_array
np.deg2rad(angles_deg, out=output_array)
# Print the results
print("Converted radians stored in output array:", output_array)
Output:
Converted radians stored in output array: [0. 0.78539816 1.57079633 3.14159265]

4. Using the where
Parameter
Using a condition to convert only selected angles.
</>
Copy
import numpy as np
# Define an array of angles
angles_deg = np.array([0, 45, 90, 180])
# Define a mask (convert only where mask is True)
mask = np.array([True, False, True, False])
# Convert only selected angles
result = np.deg2rad(angles_deg, where=mask)
# Print the results
print("Converted radians with mask:", result)
Output:
Converted radians with mask: [0. 0. 1.57079633 0. ]

The conversion is applied only to elements where mask=True
.