In this Python tutorial, I will explain to you, how to convert degrees to radians in Python.
- Degrees: A full circle is divided into 360 equal parts, each part is known as a degree.
- Radians: In radians, a full circle is equal to 2π, where π (pi) is approximately equal to 3.14159265358979323846.
The relationship between degrees and radians is given by the formula:
radians = degrees * (π / 180)
Python degrees to radians
Python has a built-in module that can be used for the mathematical task on numbers. The math module contains functions for calculating the various trigonometric ratios for a given angle.
The math module presents two angles conversion function which is degrees() and radians(). The math module has a set of methods.
In Python, math.radians() method is used to convert a degree value to radians. So, we will first import math module.
Example:
import math
print(math.radians(120))
Once you execute the code, “math.radians(120)”, will print like 2.0943951023931953.
You can refer to the below screenshot:
This is how to convert degrees to radians in Python.
Convert Degrees to Radians in Python
Here is a different way to convert degrees to radians in Python.
- The first step is to import the necessary libraries. Python’s
math
library contains a constant forπ
(pi) which we will be using.
import math
- Next, let’s create a function that takes an angle in degrees as an argument and returns its equivalent in radians.
def degrees_to_radians(degrees):
return degrees * (math.pi / 180)
- In a practical program, you might want to allow the user to input the angle in degrees. You can do this using the input() function.
degrees = float(input("Enter angle in degrees: "))
radians = degrees_to_radians(degrees)
print(f"{degrees} degrees is equal to {radians} radians")
- Python’s math library also includes a built-in function to convert degrees to radians, which is math.radians(). This can be used as an alternative to the function we created earlier.
radians = math.radians(degrees)
print(f"{degrees} degrees is equal to {radians} radians")
Below is the complete code:
import math
def degrees_to_radians(degrees):
return degrees * (math.pi / 180)
# Using custom function
degrees = float(input("Enter angle in degrees: "))
radians = degrees_to_radians(degrees)
print(f"{degrees} degrees is equal to {radians} radians using the custom function")
# Using built-in function
radians = math.radians(degrees)
print(f"{degrees} degrees is equal to {radians} radians using the built-in function")
You can check out the output like below:
Convert degrees to radians using NumPy in Python
Let’s see how to convert degrees to radians using NumPy in Python.
To convert degrees to radians in Python, we have to import numpy package first. It has an inbuilt function name deg2rad() which can directly convert degrees to radians.
Example:
import numpy as np
print("degrees to radian: ",np.deg2rad(2))
After writing the above code, Ones you will print “np.deg2rad(2)” then the output will appear as “degrees to radian: 0.03490658503988659“. Here, np.deg2rad(2) will convert degrees to radians.
You can refer to the below screenshot:
Conclusion
We can easily use the Python Math module to convert degrees to radians in Python. I have explained examples of Python degrees to radians.
You may also like:
- Convert Epoch to Datetime in Python
- Convert bool to string in Python
- How to Convert a List to a String in Python?
- Convert a List of Characters into a String in Python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.