How to Convert Angle Units in Python
In scientific computing, computer graphics, and physics simulations, handling angles correctly is critical. A frequent source of bugs in Python is passing degrees to a function that expects radians.
This guide explains how to convert between Degrees, Radians, and Gradians using Python's built-in math module for single values and numpy for high-performance array processing.
Understanding Angle Units
Before coding, it is essential to understand the mathematical relationship between the units:
- Degrees: A full circle is 360 degrees.
- Radians: A full circle is 2π (approx 6.283). This is the standard unit for Python math functions.
- Gradians: A full circle is 400 grads. Used historically in surveying.
Conversion Formula: Radians = Degrees × (π / 180)
Method 1: Using the math Module (Standard)
For single value conversions, Python's built-in math module is the most efficient tool. It provides radians() and degrees() functions.
import math
angle_deg = 45
# ✅ Correct: Convert Degrees to Radians
angle_rad = math.radians(angle_deg)
# Convert Radians back to Degrees
angle_deg_returned = math.degrees(angle_rad)
print(f"{angle_deg} degrees is {angle_rad:.4f} radians")
print(f"Converted back: {angle_deg_returned} degrees")
Output:
45 degrees is 0.7854 radians
Converted back: 45.0 degrees
Python's trigonometric functions (math.sin, math.cos, math.tan) always expect input in Radians, not Degrees. Failing to convert will result in incorrect calculations.
Method 2: Using numpy (For Arrays)
If you are working with large datasets or lists of angles, numpy is significantly faster and allows you to convert entire arrays in one line.
import numpy as np
# Array of angles in degrees
degrees_arr = np.array([0, 30, 45, 60, 90])
# ✅ Correct: Vectorized conversion to Radians
radians_arr = np.deg2rad(degrees_arr)
# Convert back to Degrees
degrees_arr_back = np.rad2deg(radians_arr)
print(f"Degrees: {degrees_arr}")
print(f"Radians: {np.round(radians_arr, 4)}")
Output:
Degrees: [ 0 30 45 60 90]
Radians: [0. 0.5236 0.7854 1.0472 1.5708]
Method 3: Handling Gradians (Custom Conversion)
Python does not have built-in support for Gradians. You must implement the conversion formula manually: Gradians = Degrees × (400 / 360)
import math
def degrees_to_gradians(deg):
return deg * (400 / 360)
def gradians_to_degrees(grad):
return grad * (360 / 400)
angle = 90
grad_val = degrees_to_gradians(angle)
print(f"{angle} degrees = {grad_val} gradians")
Output:
90 degrees = 100.0 gradians
Practical Example: Polar to Cartesian Coordinates
A common use case for angle conversion is transforming coordinates. To convert Polar coordinates (Radius r, Angle theta) to Cartesian coordinates (x, y), you must first convert theta to radians.
import math
def polar_to_cartesian(radius, angle_degrees):
# 1. Convert to Radians
angle_radians = math.radians(angle_degrees)
# 2. Apply Trigonometry
x = radius * math.cos(angle_radians)
y = radius * math.sin(angle_radians)
return x, y
r = 10
theta = 60 # degrees
x, y = polar_to_cartesian(r, theta)
print(f"Polar({r}, {theta}°) -> Cartesian(x={x:.2f}, y={y:.2f})")
Output:
Polar(10, 60°) -> Cartesian(x=5.00, y=8.66)
Conclusion
To convert angles effectively in Python:
- Use
math.radians()for single values before passing them to trig functions. - Use
numpy.deg2rad()when working with arrays or large datasets. - Implement custom logic only for non-standard units like Gradians.