1.Why should I care?
Ramps, braces, robot arms and wheels all need square roots and angles. Python's math module has them, but with one famous trap: its trigonometry works in radians. Mixing up degrees and radians has caused real engineering errors.
- ◇ Import and use math.sqrt, math.sin, math.cos, math.radians and math.pi.
- ◇ Convert degrees to radians before using trigonometric functions.
- ◇ Compute lengths and heights in simple structures, with units, and sanity-check them.
2.Discover
Choose 'Ramp height (math)'. Compare the version with math.radians to the one without, at several angles: is the wrong one ever right? Then choose 'Diagonal brace (math)' and find out why √w + √h is not the brace length.
Loading the interactive…
Can't use the interactive? Read a description instead
The precedence explorer's ramp preset compares length * math.sin(math.radians(angle)) with length * math.sin(angle). For 2.4 m at 30° the first gives 1.1999999999999997 m (matches 1.2 m) and the second gives −2.3712758978228683 m (wrong). The brace preset shows math.sqrt(0.3 ** 2 + 0.4 ** 2) = 0.5 m, while math.sqrt(w ** 2) + math.sqrt(h ** 2) gives 0.7 m.
3.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1 · a fresh version every time
A loading ramp is 2.7 m long and rises at 5° to the floor. Python computes height_m = 2.7 * math.sin(math.radians(5)). What is the height, in metres?
Practice 2
Which line correctly computes cos of 60°?
4.Predict
What does math.sin(30) give in Python?
Guess first. Being wrong here is how the surprise works.
5.Understand
Import, then call
import math
length_m = 2.4
angle_deg = 30
height_m = length_m * math.sin(math.radians(angle_deg))
print(round(height_m, 3)) # 1.2
w_m, h_m = 0.3, 0.4
brace_m = math.sqrt(w_m ** 2 + h_m ** 2)
print(brace_m) # 0.5import math once at the top, then use math. before each function.
Radians in, radians out
math.sin, math.cos and math.tan expect radians, and math.atan2 returns radians. Convert at the boundary: math.radians(deg) going in, math.degrees(rad) coming out. Name variables _deg or _rad so the unit is never in doubt.
Sanity checks for geometry
A ramp's height must be less than its length. A diagonal must be longer than either side but shorter than both added together. sin and cos are always between −1 and 1. A negative height or a brace shorter than a side means a unit or bracket error.
Explain it another way
Degrees and radians are like inches and centimetres for angles. The math module only speaks radians, so you translate first.
math functions are implemented by the platform's C maths library on IEEE doubles. math.radians(x) returns x·π/180; results carry tiny rounding errors, so sin(π/6) prints as 0.49999999999999994.
Opposite = hypotenuse × sin θ; adjacent = hypotenuse × cos θ; hypotenuse = √(a² + b²). For a 0.3 m × 0.4 m frame the brace is √(0.09 + 0.16) = √0.25 = 0.5 m.
A robot arm's tip position is x = L·cos θ, y = L·sin θ for each link. Feeding encoder angles in degrees to cos and sin would put the gripper somewhere completely different.
Derivation: Degrees to radians
A full turn is 360°. Measured as arc length on a circle of radius 1, a full turn is the circumference 2π, so 360° = 2π rad.
Divide both sides by 360: 1° = 2π/360 = π/180 rad.
So θ_rad = θ_deg × π / 180. For 30°: 30 × π / 180 = π/6 ≈ 0.5236 rad.
math.radians(30) does exactly this calculation. Then 2.4 × sin(π/6) = 2.4 × 0.5 = 1.2 m.
Worked example 1
A wheelchair ramp may rise at most 1 in 12. What angle is that, and how long a ramp reaches a 0.5 m step?
Slope = 1/12, so θ = math.degrees(math.atan(1 / 12)) ≈ 4.76°. Horizontal run = 0.5 × 12 = 6.0 m. Ramp length = math.sqrt(6.0 2 + 0.5 2) ≈ 6.02 m.
Common mix-ups
- math.sin(30) gives sin 30°.
✓ It gives sin of 30 radians. Convert with math.radians first. - √(a² + b²) = a + b.
✓ Square roots do not split over addition: √(9 + 16) = 5, not 3 + 4 = 7.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 3 · a fresh version every time
A rectangular frame is 80 cm wide and 115 cm tall. Python computes brace_cm = math.sqrt(80 ** 2 + 115 ** 2). How long is the diagonal brace, in centimetres?
Practice 4
Why is math.sqrt(w ** 2) + math.sqrt(h ** 2) not the brace length?
Practice 5
What is 45° in radians, to 3 decimal places?
7.Apply it
Write a program that prints the tip position (x, y) of a 0.25 m robot arm link for angles 0°, 30°, 60° and 90°. Check that x² + y² is always 0.25² (to float precision), then compare with the kinematics lab.
8.Reflect
9.Go further
- Python math module ↗Every function in math, with notes on units and edge cases.
Concepts: Engineering formulas as code · Angles and trigonometry