The math Module in Python: trig, roots, and constants
Beyond + and *, real geometry needs sine, cosine, roots, and pi. The math module supplies them — enough to arrange things on a circle or measure a distance.
The big idea
math provides mathematical constants and functions — math.pi, math.sin, math.cos, math.sqrt — for calculations basic operators can't do.
See it in code
The math module supplies constants and functions plain operators can't. math.pi is the circle constant; math.sqrt takes a square root:
import math
print("pi is about", math.pi)
print("sqrt(144) is", math.sqrt(144))pi is about 3.141592653589793 sqrt(144) is 12.0
import math unlocks math.pi, math.sqrt, and the trig functions — the tools real geometry needs.
The real power is sin and cos, which turn an angle into a point on a circle. This walks four evenly spaced angles and prints each (x, y) — the exact loop we're about to draw with:
import math
cx, cy = 250, 250
for i in range(4):
angle = i * (2 * math.pi / 4)
x = cx + 160 * math.cos(angle)
y = cy + 160 * math.sin(angle)
print(round(x), round(y))410 250 250 410 90 250 250 90
Each angle became an (x, y) on a circle of radius 160 centered at (250, 250). Feed those to star instead of print and you've drawn the ring.
Now draw it. Same formula, but range(12) for a fuller ring, and instead of printing we hand each (x, y) to star. cos gives the horizontal offset, sin the vertical:
import math
from art import Canvas, star
screen = Canvas.create()
Canvas.fill(screen)
cx, cy = 250, 250
for i in range(12):
angle = i * (2 * math.pi / 12)
x = cx + 160 * math.cos(angle)
y = cy + 160 * math.sin(angle)
star(screen, x, y, size=34)
cos(angle) and sin(angle) return values between -1 and 1; multiplying by the radius 160 and adding the center places each star on the ring. The same sine and cosine drive orbits, pendulums, and wave animations.
math is the backbone of anything geometric or scientific: math.sqrt and the Pythagorean theorem for the distance between two entities, math.hypot as a shortcut, math.floor/math.ceil to snap to a grid, math.log for scales. It works in radians, so remember math.radians() to convert from degrees.
Try it yourself
Compute the distance between two points with math.sqrt((x2 - x1)**2 + (y2 - y1)**2). Then change range(12) to range(24) for a denser ring, and shrink the radius to nest a second circle inside.
The common mistake
Feeding degrees to sin/cos, which expect radians. math.sin(90) is not 1 — it treats 90 as radians. Convert first with math.radians(90), or work in radians from the start as the circle example does.
What it unlocks
The math module builds on imports and operators, and powers curved motion in frames and motion.