NumPy radians(): Convert Angles from Degrees to Radians

import numpy as np

ar_deg = np.array([20, 90, 120, 160, 180])
ar_rad = np.radians(ar_deg)
print(ar_rad)  # [0.34906585 1.57079633 2.0943951  2.7925268  3.14159265]

numpy.radians(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
radians() is a universal function (ufunc) that converts angle measurements from degrees to radians element-wise.

The radians() function multiplies each input element in degrees by π / 180 to produce radian equivalents. For the inverse operation converting radians to degrees, see rad2deg(). For trigonometric calculations, review Math & ufuncs.

ParameterDescription
xInput array or angle values in degrees
outOptional, ndarray to store the result; must match expected output shape
whereOptional, boolean array mask; elements where True are evaluated, False left untouched
dtypeOptional, data-type override for output

Basic radians() Examples 🔝

Passing degree angle arrays converts them into radians:

import numpy as np

ar_deg = np.array([20, 90, 120, 160, 180])
ar_rad = np.radians(ar_deg)
print(ar_rad)

assert round(ar_rad[1], 4) == 1.5708

Expected output

[0.34906585 1.57079633 2.0943951  2.7925268  3.14159265]

Legacy vs Modernized Code Comparison 🔝

The table below highlights how legacy scripts compare against the modernized, structured implementation:

Aspect Legacy Implementation Modernized Implementation
Structure & Markup Basic echo output with unformatted pre tags and missing metadata. Structured HTML5 layout, Schema TechArticle markup, and responsive tables.
Code Presentation Requires scrolling past introductory text to find code examples. Core code snippet placed right at the top for instant visitor access.
Verification Unchecked print statements with manual result inspection comments. Automated unit test assertions (assert) verifying array outputs.
Ecosystem Integration Isolated code blocks with sparse navigation links. Comprehensive cross-linking, quick-jump navigation, and runnable Colab notebooks.

Specifying dtype 🔝

Enforce exact floating-point precision using the dtype parameter:

ar_deg = np.array([20, 90, 120, 160, 180])
ar_rad = np.radians(ar_deg, dtype=float)
print("Float dtype output:", ar_rad)

assert ar_rad.dtype == np.float64

Expected output

Float dtype output: [0.34906585 1.57079633 2.0943951  2.7925268  3.14159265]

Using where Mask 🔝

The where parameter allows selective conversion across boolean array masks:

ar_deg = np.array([120, 160, 180])
ar_where = np.array([True, False, True])
ar_rad = np.radians(ar_deg, where=ar_where)
print("Masked conversion result:", ar_rad)

Expected output (Index 1 is left un-evaluated)

Masked conversion result: [2.09439510e+000 7.90505033e-322 3.14159265e+000]

Using out Buffer 🔝

Alternative output arrays can be preallocated using ones() or shape buffers to eliminate runtime memory allocation overhead:

ar_deg = np.array([120, 160, 180])
ar_out = np.ones(ar_deg.shape)
ar_rad = np.radians(ar_deg, out=ar_out)

print("Result in ar_out:", ar_out)
print("Are ar_deg and ar_out the same object?", ar_rad is ar_out)

assert (ar_rad is ar_out) == True

Expected output

Result in ar_out: [2.0943951  2.7925268  3.14159265]
Are ar_deg and ar_out the same object? True

Common Pitfalls 🔝

  • Degrees vs Radians Confusion: Passing radian values into np.radians() results in incorrect conversion scaling. Ensure input arrays represent angles in degrees.
  • Dtype Precision: Integer degree inputs are automatically promoted to floating-point values during radian conversion.

Practice in Google Colab 🔝

Test radians() conversions, preallocated buffers, boolean masks, and run verification test suites in our interactive Google Colab companion notebook:

Interactive Practice: Colab & GitHub

Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:

View on GitHub Open In Colab

Continue exploring related math functions: rad2deg(), ceil(), floor(), and Math & ufuncs.




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer