Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
degrees() and radians() in Python
The measurements of angles in mathematics are done using two units: degrees and radians. They are frequently used in math calculations involving angles and need conversion from one value to another. In Python, we can achieve these conversions using built-in functions from the math module.
degrees() Function
This function takes a radian value as parameter and returns the equivalent value in degrees. The return type is a float value.
Syntax
math.degrees(x)
Where x is the angle in radians.
Example
import math
# Converting radians to degrees
print("1 radian in degrees:", math.degrees(1))
print("0.5 radians in degrees:", math.degrees(0.5))
print("3.14159 radians in degrees:", math.degrees(3.14159))
1 radian in degrees: 57.29577951308232 0.5 radians in degrees: 28.64788975654116 3.14159 radians in degrees: 179.99942478929447
radians() Function
This function takes a degree value as parameter and returns the equivalent value in radians. The return type is a float value.
Syntax
math.radians(x)
Where x is the angle in degrees.
Example
import math
# Converting degrees to radians
print("1 degree in radians:", math.radians(1))
print("60 degrees in radians:", math.radians(60))
print("180 degrees in radians:", math.radians(180))
1 degree in radians: 0.017453292519943295 60 degrees in radians: 1.0471975511965976 180 degrees in radians: 3.141592653589793
Using NumPy for Angle Conversion
NumPy package also has built-in functions which can directly convert degrees to radians and vice versa. These functions are named deg2rad() and rad2deg().
Example
import numpy as np
# Converting using NumPy functions
print("90 degrees to radians:", np.deg2rad(90))
print("1.57 radians to degrees:", np.rad2deg(1.57))
print("Array conversion - degrees to radians:", np.deg2rad([0, 30, 45, 90]))
90 degrees to radians: 1.5707963267948966 1.57 radians to degrees: 89.95437383553924 Array conversion - degrees to radians: [0. 0.52359878 0.78539816 1.57079633]
Comparison
| Method | Function | Module | Array Support |
|---|---|---|---|
| Degrees to Radians | math.radians() |
math | No |
| Radians to Degrees | math.degrees() |
math | No |
| Degrees to Radians | np.deg2rad() |
numpy | Yes |
| Radians to Degrees | np.rad2deg() |
numpy | Yes |
Conclusion
Use math.degrees() and math.radians() for single value conversions. For array operations or scientific computing, NumPy's deg2rad() and rad2deg() functions are more efficient and support vectorized operations.
