In this tutorial, you’ll learn how to use Python to convert degrees to radians and radians to degrees. You’ll learn what the relationship between degrees and radians is. Then you’ll learn how to use math
library and the numpy
library to use Python to convert degrees to radians and radians to degrees.
The Quick Answer: Use math.radians() and math.degrees()
Table of Contents
What are degrees and radians?
Degrees and radians are commonly used metrics that are used to represent angles. Because of their utility, its helpful to know how to convert between them.
The formula used to calculate a degree, with a known radian is:
degrees = r * (180° / pi)
The formula used to calculate a radian, with a known number of degrees:
radians = degrees * (pi / 180°)
Now that you know the relationship between degrees and radians, let’s explore how to convert between them using the math
library.
Want to learn more about calculating the square root in Python? Check out my tutorial here, which will teach you different ways of calculating the square root, both without Python functions and with the help of functions.
Use Math to Convert Degrees to Radians
The math
library comes with a helpful function, radians()
, that allows us to convert between degrees and radians. The math.radians()
function takes a single input, a value that represents a degree, and returns a value that represents a radian.
Let’s try a few practical examples. We know, based on the formula, that if we were to pass in the value of pi
, that we should be able to return the value of 180
.
Let’s give this a shot by using the math
library’s pi
constant, which can be accessed by math.pi
:
# Use math to convert degrees to radians
import math
print(math.degrees(math.pi))
# Returns: 180.0
We can see here that the correct value is returned. Now let’s try passing in another value:
# Use math to convert degrees to radians
import math
print(math.degrees(3))
# Returns: 171.88733853924697
In the next section, you’ll learn how to use the math
library to convert from radians to degrees.
Want to learn more about Python for-loops? Check out my in-depth tutorial that takes your from beginner to advanced for-loops user! Want to watch a video instead? Check out my YouTube tutorial here.
Use Math to Convert Radians to Degrees
Similar to how the math
library has a function to convert radians into degrees, we can use the radians
function to convert degrees into radians.
We know, based on the formula, that if we pass in the value of 180
degrees, that the value of pi
should be returned.
Let’s try doing this using Python:
# Use math to convert radians to degrees
import math
print(math.radians(180))
# Returns: 3.141592653589793
Now that we have confirmed this, let’s try passing in another value:
# Use math to convert radians to degrees
import math
print(math.radians(90))
# Returns: 1.5707963267948966
In the next section, you’ll learn how to convert degrees to radians using Python and numpy.
Want to learn more about Python list comprehensions? Check out this in-depth tutorial that covers off everything you need to know, with hands-on examples. More of a visual learner, check out my YouTube tutorial here.
Use Numpy to Convert Radians to Degrees
Similar to how the math
library comes with built-in functions to convert between radians and degrees, the popular numerical library numpy
comes with similar functions.
In order to use numpy
to convert from degrees to radians, we can use the radians()
function.
Let’s try again to convert the value of pi
to see if it returns the value of 180:
# Use numpy to convert radians to degrees
import numpy as np
import math
print(np.degrees(math.pi))
# Returns: 180.0
Now let’s try another numpy to see its result:
# Use numpy to convert radians to degrees
import numpy as np
print(np.degrees(3))
# Returns: 171.88733853924697
In the next section, you’ll use numpy to convert radians to degrees.
Need to check if a key exists in a Python dictionary? Check out this tutorial, which teaches you five different ways of seeing if a key exists in a Python dictionary, including how to return a default value.
Use Numpy to Convert Degrees to Radians
Similar to using numpy to convert degrees to radians, we can also use numpy to convert radians to degrees. For this, we’ll use the np.radians()
function. This function takes a single input, a value that represents radians, and returns a single output, a value that represents degrees.
Let’s pass in the value of 180 again and see if it returns the value of pi
:
# Use numpy to convert degrees to radians
import numpy as np
print(np.radians(180))
# Returns: 3.141592653589793
Now that that’s confirmed, let’s check another value:
# Use numpy to convert degrees to radians
import numpy as np
print(np.radians(90))
# Returns: 1.5707963267948966
Want to learn how to pretty print a JSON file using Python? Learn three different methods to accomplish this using this in-depth tutorial here.
Conclusion
In this tutorial, you learned how to use Python to convert radians to degrees and degrees to radians. You also learned how to use both the math
and numpy
libraries to convert radians to degrees.
To learn more about the math
library, check out the official documentation here.