Use radians correctly with trigonometric functions.
What It Does and Why It Matters
The functions math.sin(), math.cos() and math.tan() expect angles in radians, not degrees. Convert degree inputs with math.radians(); convert results such as atan2() back with math.degrees() when presenting them to users. Functions near angles where tangent is undefined can yield very large floating-point approximations.
Use math.hypot() for distances and math.atan2(y, x) for quadrant-aware angles.
Step-by-Step Example
Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.
import math
angle_degrees = 30
angle_radians = math.radians(angle_degrees)
print(round(math.sin(angle_radians), 3))
print(round(math.cos(angle_radians), 3))
print(round(math.degrees(math.atan2(1, 1)), 1))
How the Code Works
- The imports make these standard-library tools available: math.
- The name angle_degrees receives the value of 30. Changing this input lets you explore a different case.
- The print calls display the results. Compare these results with the method’s documented return type and the input used.
Expected output:
0.5
0.866
45.0
Each displayed line comes from the corresponding calculation or check. The output helps verify the return values before you adapt the example to your own input.
What to Notice
Pay attention to the accepted input types, the returned value and the boundary behavior. Predict what changes if you replace the example input with an empty, zero, negative or missing value, where that value is meaningful for this operation.
If the task uses a library function, inspect its documented exceptions and return type before reusing it inside a larger function.
Where You Would Use This
Use the technique when it makes a real task easier to understand or safer to maintain. Check input and output behavior with small tests before combining it with larger application code.
Practice Exercise
Calculate the horizontal and vertical components of a length-10 vector at 60 degrees.
Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.
Reference: official Python documentation for the complete API contract and version notes.
Common Beginner Mistakes
- Calling math.sin(30) while assuming 30 means degrees.
- Overlooking the documented condition: the functions math.sin(), math.cos() and math.tan() expect angles in radians, not degrees.
Key Takeaways
- Use radians correctly with trigonometric functions.
- The functions math.sin(), math.cos() and math.tan() expect angles in radians, not degrees.
- Calculate the horizontal and vertical components of a length-10 vector at 60 degrees.