Convert Degrees to Radians in Python and vice versa

How to Convert Degrees to Radians in Python and vice versa

Popular Domains for just 99 Cents at Namecheap!

Radians are mostly used when you want to do mathematical calculations on a Circular object or path. Python has inbuilt functions that can convert radians to degrees and degrees to radians. In this entire tutorial, you will know how to convert radians to degrees and vice-versa in python using various examples.

How to Convert Degrees to radians in Python

In this section, you will know to convert radians to degrees in python. Suppose I want to use degree inside the NumPy cos() method.  As this function accepts only radian values so I have to convert it to radians. There is a python module that allows this conversion and it is math.radians().

import math
import numpy as np
radians = math.radians(30)
print(np.cos(radians))

The above code first converted the degrees to radians and then the results are passed as an argument to the numpy.cos() method. You will get the following output when you will run the code.

Output

conversion of degrees to radians in python
conversion of degrees to radians in python

The other method to convert degrees to radians is to define your own custom function. Inside the function, you will use the radians conversion formulae. The following are the lines of code for the function.

import math
import numpy as np

def to_radian(degree):
return degree * math.pi/180

print(np.cos(to_radian(30)))

You can see in the function I am taking degree as a parameter and converting it to radians using formulae degree * pi/180.

You will get the same output as the first method.

Output

conversion of degrees to radians in python using custom function
conversion of degrees to radians using custom function

Convert Radians to Degrees

Python module also provides a method to convert radians to degrees. And that function is math.degrees(). Inside this method, you will put radian value and it will convert radian to degrees.  Let’s take the output of the radian of the above methods (30) and convert it back to degrees.

Execute the below lines of code.

import math
import numpy as np
radians = math.radians(30)
degrees = math.degrees(radians)
print(degrees)

In the above code, you can see I am taking radian input and passing it to the degrees() method. It will convert back the radian to degrees. You will get the following output.

Converting radians to degrees in python
Converting radians to degrees

These are examples of how to convert degree to radians in python and vice-versa. I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Share via
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner