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]) 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.
| Parameter | Description |
|---|---|
x | Input array or angle values in degrees |
out | Optional, ndarray to store the result; must match expected output shape |
where | Optional, boolean array mask; elements where True are evaluated, False left untouched |
dtype | Optional, data-type override for output |
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]
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. |
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]
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]
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
np.radians() results in incorrect conversion scaling. Ensure input arrays represent angles in degrees.Test radians() conversions, preallocated buffers, boolean masks, and run verification test suites in our interactive Google Colab companion notebook:
Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:
View on GitHubContinue exploring related math functions: rad2deg(), ceil(), floor(), and Math & ufuncs.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.