Degrees to Radians Conversion Python
- Degrees
- 180 °
- You entered
To convert degrees to radians in Python, use the math.radians() function from the built-in math module, which returns the angle in radians as a float. Alternatively, multiply the degree value by math.pi / 180 to get the same result manually.
Formula
The conversion formula is:
radians = degrees × (π / 180)
Where:
- degrees – the angle in degrees (unit: °)
- radians – the resulting angle in radians (unit: rad, dimensionless)
- π (pi) ≈ 3.141592653589793
This formula comes from the fact that a full circle is 360° and also 2π radians, so 180° = π radians.
Worked Example
Using the default value of 180 degrees:
- Start with the formula: radians = degrees × (π / 180)
- Substitute 180 for degrees: radians = 180 × (π / 180)
- Simplify: radians = π
- Numeric value: radians ≈ 3.141593
The calculator above gives 3.141593 rad, matching exactly.
How to Use math.radians()
The math module is part of Python’s standard library, so no extra installation is needed. Here’s a simple example:
import math
angle_deg = 45
angle_rad = math.radians(angle_deg)
print(angle_rad) # 0.7853981633974483
You can also pass a list comprehension to convert multiple values at once:
import math
degrees_list = [0, 30, 60, 90]
radians_list = [math.radians(d) for d in degrees_list]
print(radians_list) # [0.0, 0.5235987755982988, 1.0471975511965976, 1.5707963267948966]
The function works with integers and floats, and returns a float. For very large or very small numbers, it handles them without special issues.
Manual Conversion: Multiplying by pi/180
If you prefer not to use the math module, you can do the conversion directly:
import math
angle_deg = 180
angle_rad = angle_deg * math.pi / 180
print(angle_rad) # 3.141592653589793
This is exactly what math.radians() does internally. You can also use a literal approximation of π, but for precision, always use math.pi.
Note that the expression angle_deg * (math.pi / 180) is equivalent. Order of operations doesn’t change the result because multiplication and division have the same precedence and are left-associative.
Common Mistakes and Pitfalls
- Forgetting to import math – always
import mathbefore usingmath.radians()ormath.pi. - Confusing degrees and radians in trig functions –
math.sin(),math.cos(), etc. expect radians, not degrees. If you pass degrees, you’ll get wrong results. - Using integer division – in Python 2,
180 / 180is an integer, but with floats it’s fine. In Python 3, division always returns a float. If you’re using a custom formula, ensure at least one operand is a float. - Rounding too early – if you round the intermediate π/180 factor, you lose precision. Keep full precision until the final result.
- Not using math.radians() for arrays – for NumPy arrays, use
numpy.radians()instead, which works element-wise.
When to Use Which Method
Use math.radians() when you need a quick, readable conversion for a single value or a small list. It’s the most straightforward and less error-prone.
Use the manual multiplication deg * math.pi / 180 when you want to combine the conversion with other arithmetic in a single expression, or when you need to avoid an extra function call in performance-critical loops (though the difference is negligible).
If you’re working with NumPy arrays, use numpy.radians() for vectorized conversion. For example:
import numpy as np
arr_deg = np.array([0, 45, 90])
arr_rad = np.radians(arr_deg)
print(arr_rad) # [0. 0.78539816 1.57079633]
This is much faster than a Python loop for large arrays.
Why Python Uses Radians
Radians are the natural unit for angles in mathematics because they simplify many formulas. For instance, the derivative of sin(x) is cos(x) only when x is in radians. In degrees, you’d have a factor of π/180. Python’s math library follows this convention, so all trigonometric functions expect radians. This is why you often need to convert degrees (which are more intuitive for humans) to radians before using them in calculations.
Frequently Asked Questions
How do I convert radians back to degrees in Python?
Use math.degrees(radians) to convert from radians to degrees. For example, math.degrees(math.pi) returns 180.0. You can also multiply by 180/π manually.
What is the difference between math.radians and numpy.radians?
math.radians works on a single number (int or float) and returns a float. numpy.radians works element-wise on arrays and also accepts a scalar, returning a NumPy array or scalar. For large datasets, NumPy is faster.
Can I use degrees directly in math.sin()?
No. math.sin() expects the argument in radians. If you have degrees, you must convert first: math.sin(math.radians(30)) gives 0.5, the correct sine of 30°.
Is there a built-in constant for π in Python?
Yes, math.pi gives the floating-point approximation of π (3.141592653589793). There is also math.tau (2π) in Python 3.6 and later.
Why do I get a slightly different result when I use 22/7 instead of math.pi?
22/7 is a rational approximation of π (about 3.142857) and differs from the true value by about 0.04%. Using math.pi gives a much more precise value, which matters in scientific and engineering calculations.
How do I convert degrees to radians without importing math?
You can define your own pi constant, for example pi = 3.141592653589793, and then multiply degrees by pi / 180. However, using the math module is recommended because it provides a more accurate and consistent value.
Related
Formula used
in deg | Degrees | ° | 180 | 0 out rad | Radians | rad | deg * pi / 180 | 6
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.