degrees(function python
degrees(function python
In Python, we often need to convert angles. Sometimes we need to convert radians to degrees, and sometimes we need to convert degrees to radians. In Python, we can easily perform both conversions, mainly with the help of the math module radians() and degrees() functions.
Radians() Function
The radians() function is used to convert an angle to radians. Its prototype is as follows:
import math
math.radians(x)
Where x represents the angle to be converted.
The following is a sample code demonstrating how to use the radians() function to convert an angle to radians:
import math
angle_degrees = 45
angle_radians = math.radians(angle_degrees)
print(f"{angle_degrees} degrees is equal to {angle_radians} radians")
Running the above code produces the following output:
45 degrees is equal to 0.7853981633974483 radians
degrees() Function
The degrees() function is used to convert radians to degrees. Its prototype is as follows:
import math
math.degrees(x)
Where x represents the radians to be converted.
The following is a sample code demonstrating how to use the degrees() function to convert radians to degrees:
import math
angle_radians = 1.57
angle_degrees = math.degrees(angle_radians)
print(f"{angle_radians} radians is equal to {angle_degrees} degrees")
Running the above code produces the following output:
1.57 radians is equal to 89.95437383553924 degrees
These two functions allow us to easily convert between degrees and radians, making mathematical calculations and scientific research more convenient. In actual programming, we often use these two functions to handle angle-related calculations, so mastering their usage is extremely important.