math.degrees() function in Python is a built-in function that is part of the
math module. It allows us to converts an angle from radians to degrees.
The Python Math Module
math module in Python provides a set of mathematical functions, constants, and implementations that allow us to perform mathematical tasks that go beyond the basic arithmetic provided by Python.
To use the
math module, need to import it using the
Once import, we can access all the functions and constants provided by the
Python Degrees Function Syntax
The syntax for the
math.degrees() function is as shown below:
Where the parameter
x refers to the angle in radians that you wish to convert into degrees.
Function Return Value
The function will return a floating-point number where denotes the degrees.
Let us explore some basic examples on how we can use this function in Python.
Example 1 - Converting Radians to Degrees
Suppose we have an angle of 1 radian, and we wish to convert it into degrees, we can use the
degrees() function as shown:
import math radians = 1 degrees = math.degrees(radians) print(degrees)
Example 2 - Converting π Radians to Degrees
In math, the value of π radians is equivalent to 180 degrees. We can confirm this by using the degrees function as demonstrated below:
import math radians = math.pi degrees = math.degrees(radians) print(degrees)
Example 3 - Converting Negative Radians to Degrees
We can also use the
math.degrees() function with negative radians. An example is as shown below:
import math radians = -1 degrees = math.degrees(radians) print(degrees)
NOTE: The input to
math.degrees() must be a number (integer or float). If you pass an argument of a different type, you will get a
import math radians = '' degrees = math.degrees(radians) print(degrees)
In this case, the function will raise a
TypeError as the input is a
TypeError Traceback (most recent call last) --- 1 import math 3 radians = '' ----> 4 degrees = math.degrees(radians) 6 print(degrees) TypeError: must be real number, not str
That's a comprehensive guide to the
math.degrees() method in Python. This function is an essential tool when working with angles, especially in fields such as physics, engineering, and computer graphics, where you often need to convert between degrees and radians. Happy coding!