Development

Python math.degrees() Method

Captain Salem 2 min read

Python math.degrees() Method

The 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.

In this tutorial, we will go through the details of using this method including the function syntax, parameters, return values, and practical examples on how to use it.

The Python Math Module

The 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 import keyword.

import math

Once import, we can access all the functions and constants provided by the math module.

Python Degrees Function Syntax

The syntax for the math.degrees() function is as shown below:

math.degrees(x)

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.

Using the math.degrees() Function

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)

Result:

57.29577951308232

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)

Output:

180.0

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)

Output:

-57.29577951308232

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 TypeError.

import math

radians = ''
degrees = math.degrees(radians)

print(degrees)

In this case, the function will raise a TypeError as the input is a str.

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

Conclusion

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!

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.