Last modified: Dec 29, 2024 By Alexander Williams

Python math.radians(): Convert Degrees to Radians

In mathematical calculations, particularly in trigonometry, we often need to convert angles from degrees to radians. Python's math.radians() function makes this conversion simple and accurate.

Understanding math.radians()

The math.radians() function takes an angle value in degrees as an argument and returns its equivalent in radians. This conversion is essential when working with trigonometric functions like sin(), cos(), and tan().

Basic Syntax


import math
result = math.radians(x)  # where x is the angle in degrees

Simple Example


import math

# Converting common angles to radians
angle_30 = math.radians(30)
angle_45 = math.radians(45)
angle_90 = math.radians(90)

print(f"30 degrees = {angle_30:.4f} radians")
print(f"45 degrees = {angle_45:.4f} radians")
print(f"90 degrees = {angle_90:.4f} radians")


30 degrees = 0.5236 radians
45 degrees = 0.7854 radians
90 degrees = 1.5708 radians

Working with Lists of Angles


import math

# Converting multiple angles using list comprehension
angles_degrees = [0, 30, 45, 60, 90, 180]
angles_radians = [math.radians(angle) for angle in angles_degrees]

# Display results in a formatted way
for deg, rad in zip(angles_degrees, angles_radians):
    print(f"{deg:3d} degrees = {rad:.4f} radians")


  0 degrees = 0.0000 radians
 30 degrees = 0.5236 radians
 45 degrees = 0.7854 radians
 60 degrees = 1.0472 radians
 90 degrees = 1.5708 radians
180 degrees = 3.1416 radians

Practical Applications

The conversion from degrees to radians is crucial in many mathematical and scientific applications. Here's a practical example using trigonometric functions:


import math

def calculate_triangle_height(angle_degrees, base_length):
    """Calculate triangle height using angle and base length"""
    # Convert angle to radians for math operations
    angle_radians = math.radians(angle_degrees)
    # Calculate height using tangent
    height = base_length * math.tan(angle_radians)
    return height

# Example usage
angle = 45
base = 10
height = calculate_triangle_height(angle, base)
print(f"Triangle height with {angle}° angle and base {base}: {height:.2f} units")


Triangle height with 45° angle and base 10: 10.00 units

Common Pitfalls and Tips

Remember that Python's trigonometric functions expect angles in radians. Forgetting to convert degrees to radians is a common source of errors in calculations.

Avoiding Precision Errors


import math

# Demonstrating precision handling
angle = 180
radians = math.radians(angle)
print(f"Original angle: {angle}°")
print(f"In radians: {radians}")
print(f"Rounded to 6 decimal places: {round(radians, 6)}")  # π ≈ 3.141593


Original angle: 180°
In radians: 3.141592653589793
Rounded to 6 decimal places: 3.141593

Converting Back to Degrees

When you need to convert radians back to degrees, you can use the complementary function math.degrees().

Conclusion

The math.radians() function is an essential tool for mathematical computations in Python, especially in trigonometry and physics calculations.

Understanding how to properly use this function can help avoid common calculation errors and make your mathematical operations more accurate and reliable.