Sine and cosine turn an angle into two numbers: how far across and how far up an arm of length 1 reaches at that angle. That makes them the bridge between turning and coordinates. They're how a direction becomes a pair of numbers, how a picture is rotated, and, in the next lesson, how waves are made. Only two facts are needed to start, and both come from a right triangle. By the end you'll compute them in Python, draw a circle from them point by point, and rotate a shape the way an image model rotates its training pictures.
What you'll be able to do
Read sine and cosine as ratios of a right triangle's sides
Place the point at angle θ on the unit circle as (cos θ, sin θ), with its signs by quadrant
Compute sin and cos in Python, and generate points around a circle with a loop
Turn an angle into a direction of length 1, and rotate a point, as models do
Quick check
Have a guess before you start. A ladder 10 m long leans at 30° from the ground. Roughly how high does it reach?
10 m
3 m
5 m
8.7 m
Show the answer
5 m
The height is 10 × sin 30° = 10 × 0.5 = 5 m. At 30°, the far end rises exactly half the ladder's length.
The math: across and up
Purpose. Turn an angle into an across-and-up pair of numbers.
In a right triangle, pick one of the two sharp angles, θ. The side across from it is the opposite, the side next to it (not the long one) is the adjacent, and the long side is the hypotenuse. Then:
sinθ=hypotenuseoppositecosθ=hypotenuseadjacent
In plain words: sine is how much of the long side goes up; cosine is how much goes across.
In a 3-4-5 triangle with θ at the corner by the side of 4, sin θ = 3/5 = 0.6 and cos θ = 4/5 = 0.8.
Quick check
From an earlier lesson: what is 60° in radians?
60π
π/6, about 0.524
1/6
π/3, about 1.047
Show the answer
π/3, about 1.047
60° is a sixth of a turn, so it's 2π/6 = π/3 ≈ 1.047. Python's sin and cos need it in this form.
The unit circle. Make the hypotenuse exactly 1: an arm of length 1 turning from pointing right. At angle θ its tip is at (cos θ, sin θ), because with a hypotenuse of 1, the across and up sides are the cosine and sine. This works for every angle, not just the ones in a triangle. Watch the arm at 60°. The picture turns it and writes its angle in radians. Then it draws the across leg, then the up leg: the two short sides of a right triangle whose long side is the arm. Last it squares both legs.
Try it now
Then set the angle in degrees to 150, then 240: in the left half the across leg, cos, is negative, and in the bottom half the up leg, sin, is.
Symbols and formula. The identity, and the signs:
sin2θ+cos2θ=1
In plain words: across squared plus up squared is the arm squared, and the arm is 1.
Going around the circle, the signs follow the quadrants: on the right, cos is positive; on the left, negative. Above the middle, sin is positive; below, negative. So at 210° (down and left) both are negative: cos 210° ≈ −0.866 and sin 210° = −0.5. A handful of values are worth knowing: sin 30° = 0.5, cos 60° = 0.5, and sin 90° = cos 0° = 1.
Checking an answer with the identity. Any pair you compute for the same angle must pass sin² + cos² = 1. At 210°: 0.866² + 0.5² = 0.75 + 0.25 = 1. If a pair fails the check, one of the two numbers is wrong, often a sign or a degrees-for-radians slip. Because both are at most 1 in size, a sine or cosine of 1.2 is always a mistake.
Quick check
In a 5-12-13 right triangle, what's sin θ for the angle across from the side of 5?
12/13
5/12
13/5
5/13
Show the answer
5/13
Sine is opposite over hypotenuse: the side across from θ is 5, and the hypotenuse is 13.
Quick check
At 135° on the unit circle, what are the signs of cos and sin?
both negative
cos positive, sin negative
cos negative, sin positive
both positive
Show the answer
cos negative, sin positive
135° points up and to the left: left makes the across (cos) negative, up makes sin positive.
In your own words
In your own words: why is the point at angle θ on the unit circle exactly (cos θ, sin θ)?
Write a sentence or two, then compare it with a good answer in the lesson.
In Python: math.sin and math.cos
Purpose. Python computes sine and cosine for any angle, given in radians.
import math
r = math.radians(60)
print(round(math.cos(r), 4), round(math.sin(r), 4))
print(round(math.sin(math.radians(210)), 3), round(math.cos(math.radians(210)), 3))
It runs right here in your browser, with nothing to install.
A loop walks the arm around the circle, giving one point per angle. A third number in range is the step, so range(0, 181, 45) counts 0, 45, 90, 135 and 180:
import math
for deg inrange(0, 181, 45):
r = math.radians(deg)
print(deg, round(math.cos(r), 3), round(math.sin(r), 3))
The other half of the circle, from 180° to 360°, mirrors these with sin negative. Rounding keeps the output readable: without it, cos 90° prints as a tiny number like 6e-17 instead of 0, the float rounding you met in lesson 11.
Now go all the way around. The code flow below runs the same loop over eight angles, 0° to 315°, and keeps each point in a list. Press Next and watch points grow by one (across, up) pair each time through the loop.
Few points or many. Joining the points with straight lines draws the circle. Four points make a square, eight an octagon, and the more points you take, the rounder it looks. That's how screens draw every circle: many short straight lines. The same choice, how many points to sample, comes back in the next lesson as the sampling of waves.
A named mistake: degrees again.math.sin(30) is the sine of 30 radians, not 30°. Every angle from people goes through math.radians first. A quick check that catches it: math.sin(math.radians(90)) must be 1.
Sine and cosine as graphs. Turn the arm all the way around in steps of 15° and plot both parts against the angle. Given a list of pairs, plt.plot draws one line through the first number of every pair and another through the second, so the cosines and the sines each get a curve:
import math
import matplotlib.pyplot as plt
degs = range(0, 361, 15)
pairs = []
for d in degs:
pairs.append((math.cos(math.radians(d)), math.sin(math.radians(d))))
plt.plot(degs, pairs)
plt.xlabel("angle in degrees")
plt.ylabel("cos θ and sin θ")
plt.show()
The cosine curve starts at 1, because at 0° the arm points straight across; the sine curve starts at 0. Both swing between 1 and −1, and at 360° both are back where they began: a full turn brings the arm home. These two waves are the subject of lesson 44.
Quick check
Read the plot. Between 0° and 180°, at which angle do the two curves cross?
180°
0°
90°
45°
Show the answer
45°
Where the curves cross, cos and sin are equal: the arm points halfway between across and up. That's 45°, where the loop above printed 45 0.707 0.707.
Worked example
Worked example: eight points, each checked
Build the same eight points at 0°, 45°, …, 315°, this time without rounding, and check that each one is exactly 1 from the center.
import math
points = []
for deg inrange(0, 360, 45):
r = math.radians(deg)
points.append((math.cos(r), math.sin(r)))
all_on_circle = Truefor p in points:
ifabs(p[0] ** 2 + p[1] ** 2 - 1) > 1e-12:
all_on_circle = Falseprint(len(points), all_on_circle)
In the lesson you can change this and run it.
range(0, 360, 45) counts in steps of 45, giving eight angles. Each becomes a tuple (cos, sin). The check uses the identity sin² + cos² = 1 with a small tolerance, because floats are stored a hair off. All eight pass: every (cos θ, sin θ) is 1 from the center, whatever θ is.
Quick check
What does this print?
import math
r = math.radians(90)
print(round(math.cos(r), 3), round(math.sin(r), 3))
0.0 1.0
90 90
1.0 0.0
0.0 0.0
Show the answer
0.0 1.0
At 90° the arm points straight up: no across, all up. Rounding turns the tiny float for cos into 0.0.
Quick check
Which line gives the up part of an arm at 45°?
math.sin(45)
math.cos(math.radians(45))
45 / 90
math.sin(math.radians(45))
Show the answer
math.sin(math.radians(45))
Up is sine, and the angle must be in radians.
Drill
Points on a circle
Fix the loop so points holds the 12 points at 0°, 30°, …, 330° on the unit circle as (cos, sin) tuples.
import math
points = []
for deg inrange(0, 360, 45):
r = deg
points.append((math.sin(r), math.cos(r)))
print(len(points), points[1])
In the lesson, your code is checked as you write it.
In your own words
Why does a computer draw a circle as many short straight lines?
Write a sentence or two, then compare it with a good answer in the lesson.
In AI and ML: directions and rotations
Purpose. A direction in the plane is a pair of numbers, and cos and sin make it.
The arrow at angle θ with length 1 is (cos θ, sin θ). It's called a unit vector: all direction, no size. Models compare directions constantly, and lesson 51 shows that the cosine of the angle between two arrows measures how alike they are.
Rotation. Turning a point (x, y) by an angle θ around the origin uses the same two numbers:
x′=xcosθ−ysinθy′=xsinθ+ycosθ
In plain words: the new across and up are each a mix of the old ones, weighted by cos and sin.
import math
x = 3
y = 4
t = math.radians(90)
x2 = x * math.cos(t) - y * math.sin(t)
y2 = x * math.sin(t) + y * math.cos(t)
print(round(x2, 3), round(y2, 3))
print(math.sqrt(x ** 2 + y ** 2), round(math.sqrt(x2 ** 2 + y2 ** 2), 3))
The point (3, 4), turned a quarter turn, lands at (−4, 3), and both are 5 from the origin. Press Next to watch it turn.
Quick check
What does this print?
import math
t = math.radians(180)
print(round(math.cos(t), 3), round(math.sin(t), 3) + 0)
-1.0 -1.0
-1.0 0.0
0.0 -1.0
1.0 0.0
Show the answer
-1.0 0.0
At 180° the unit arrow points straight left: (−1, 0). Turning (1, 0) by a half turn gives its opposite.
Why models rotate data. Image models are often trained on rotated copies of their pictures, so a sign tilted by a few degrees is still recognized. Each pixel's position is turned with exactly the formula above. And some language models, using a method called rotary position encoding, turn pairs of numbers by angles that depend on a word's position, the same rotation applied many times over.
Rotation never changes a length. You saw (3, 4) stay 5 from the origin, and any other angle would do the same, because the rotation formula is built from a cosine and a sine whose squares add to 1. That's why a rotated picture keeps its shape: every pixel moves, but no distance between pixels changes.
In the real tools. PyTorch, the library most models are built with, has the same two functions. They take radians too, and work on a whole tensor of angles at once:
Drill
Directions for four angles
Write direction(deg) returning the unit vector (cos, sin) as a tuple, each rounded to 3 places. Then set opposite to whether direction(30) and direction(210) point in opposite directions.
import math
defdirection(deg):
r = math.radians(deg)
return (round(math.sin(r), 3), round(math.cos(r), 3))
a = direction(30)
b = direction(210)
opposite = a == b
print(a, b, opposite)
In the lesson, your code is checked as you write it.
In your own words
What does (cos θ, sin θ) describe, and why is its length always 1?
Write a sentence or two, then compare it with a good answer in the lesson.
31Formulas with fractions: clear them firstFreeClearing denominators, cross-multiplying, a letter in the bottom, and reciprocal formulas like 1/R = 1/R₁ + 1/R₂.35 min
37Inverse functions, lines and scalingFreeUndoing a function by making x the subject, domains that allow it, line forms, and unscaling predictions.40 min
38Sequences and sums: find n, a, d and rFreeEvery letter of the arithmetic and geometric formulas as the subject, and an average's window from its decay.40 min
40Review: rearrange anything (algebra)FreeBroken rearrangements to fix, mixed practice from the whole algebra block, a strategy chart, and its formula sheet.35 min
41Coordinates and distanceFreePoints as (x, y), Pythagoras, distance in any number of coordinates, and nearest neighbors.42 min
42Angles, degrees and radiansFreeMeasuring a turn, converting units, arcs, and the angle between two arrows.36 min
43Sine and cosineFreeAcross and up: right triangles, the unit circle, and directions and rotations.42 min
44Waves: height, speed and shiftFreeAmplitude, period and phase, sampling a wave, and position codes in transformers.37 min
68Vector equations: rearranging with arrowsFreeSolving for a vector or a scale, recovering a gradient from two weight snapshots, and an angle from a dot product.40 min
87Counting: choices, orders and groupsFreeThe counting principle, factorials, ordered picks and groups, and why attention's cost grows with the square of the length.43 min
88Probability: from counts to chancesFreeEqually likely outcomes, the complement, distributions, seeded simulation, and a next-word model.43 min
89Combining events: and, or, independentFreeThe or rule, independence, probability trees, drawing with and without replacement, and chains of steps.42 min
100Maximum likelihoodFreeLikelihoods as products, log-likelihoods as sums, grid search, underflow, and the negative log-likelihood loss.45 min
101Linear regression: the best lineFreeResiduals, least squares by formula, three roads to one line, the matrix form, and train versus test error.45 min
109One neuron, start to finishFreeWeighted sum, activation, loss, slopes and update, by hand and in code, and what one straight line can and can't split.45 min
111Training a small networkFreeThe backward pass in matrix form, gradient checks, a full training loop, and reading loss curves.45 min
112The same network in PyTorchFreenn.Sequential with the NumPy weights loaded, the same loss and slopes, then SGD and mini-batches.45 min
113Attention as matrix mathFreeScaled dot-product scores, the causal mask, a row-by-row softmax, and blending value vectors.45 min
114Residual connections and layer normFreeAdding the input back and why its slope keeps learning alive, layer norm by hand, and add and norm.40 min
115A transformer block, start to finishFreeA sentence through embeddings, attention, add and norm, a small network and next-word scores, every shape named.45 min
117Final test: transpose any formulaFreeSlips to fix from every topic, mixed practice of every kind of rearranging, the strategy chart and the full sheet.45 min
Learn the math behind AI from arithmetic up: numbers, algebra, vectors and matrices, calculus, probability and statistics, each shown as numbers moving and checked in Python, ending with a network you build and train from the formulas.
See the course →
Never trained an AI? Teach a small one to write, right in your browser. It takes about 20 seconds and needs no account. Teach it to write