mathModule in Python to Convert Degree to Radian and Vice-Versa
numpyModule to Convert Degree to Radian and Vice-Versa
Degrees and radians are two of the highly used units to represent angles. The representation between these two units is shown below.
In this tutorial, we will discuss how to convert degree to radian and vice-versa.
math Module in Python to Convert Degree to Radian and Vice-Versa
It is straightforward to implement their relations manually in Python. We can use the
math library for the required constants if we are not familiar with them. For example,
print((math.pi/2) * 180.0 / math.pi) #Rad to Deg print(90 * math.pi / 180.0 ) #Deg to Rad
Note that the
math.pi returns the mathematical constant pi and can be replaced by its value 3.141592….
We can also use different functions from the
math library to carry out these conversions.
math.degrees() function will convert the radian value passed to it into degree. For example,
import math print(math.degrees(math.pi/2))
math.radians() function will do the opposite and convert the degree value to radians. For example,
import math print(math.radians(90))
numpy Module to Convert Degree to Radian and Vice-Versa
numpy module is also equipped with different functions to convert between radian and degree values. The
numpy.degrees() function converts radians to degree and can accept an array or list of values at once.
numpy.radians() function converts degree to radians and can also accept an array or list of values.
The following code shows an example of these two functions.
import numpy lst1 = [math.pi/2, math.pi] print(numpy.degrees(lst1)) #Rad to Deg lst2 = [90,180] print(numpy.radians(lst2)) #Deg to Rad
[ 90. 180.] [1.57079633 3.14159265]
This module also has
rad2deg() functions used to perform the same function but have a more descriptive name.