Multiplying is counting equal groups fast. Dividing is the reverse: sharing things out into equal groups. Training an AI model leans on both: thousands of examples are cut into equal batches, with a few left over. This lesson shows what multiplying and dividing mean as groups, what a remainder is, and the four Python signs that do this work.
What you'll be able to do
Read multiplication as equal groups and division as sharing into groups
Split a number into a quotient and a remainder, and check it by multiplying back
Use *, /, // and % in Python, and tell a whole-number answer from a decimal one
Work out how many batches a training set makes, and how many are left over
Quick check
Have a guess before you start. 23 hikers ride in vans that seat 5. How many vans are completely full?
3
4
4.6
5
Show the answer
4
5 vans of 5 would need 25 hikers, so only 4 vans fill up (20 hikers), with 3 hikers left over for a fifth, partly empty van. Full groups and what's left over: that's division with a remainder.
The math: equal groups, and what's left over
Purpose. Counting one at a time is slow. If you know the things come in equal groups, you can count the groups instead.
Picture a tray of trail cookies laid out in rows: 4 rows of 6 cookies. That's 6 + 6 + 6 + 6, or 4 × 6 = 24.
Dividing asks the question backward: 24 cookies, 6 per bag, how many bags? 24 ÷ 6 = 4. Division undoes multiplication, so you can check a division by multiplying back: 4 × 6 = 24.
Now turn the tray sideways. It's 6 rows of 4: still 24. The order of a multiplication doesn't change the answer.
The picture draws the tray as squares, one per cookie. Press Next to bag the cookies, then again to turn the tray sideways.
Try it now
Things don't always share out evenly. 23 hikers in vans of 5 fill 4 vans with 3 hikers left over. The 4 is the quotient: how many full groups. The 3 is the remainder: what's left, always less than one group.
The flow below seats the hikers. Its picture lays them out in rows of 5, one row per van: watch the full rows light up first, then the short last row that holds the remainder.
Symbols and formula. Every whole-number division splits a number into full groups and a remainder:
a=q×b+r
In plain words: the number you started with is the number of full groups times the group size, plus what's left over. The remainder r is always smaller than the group size b.
Worked example: 29 ÷ 4. Four fits into 29 seven times (7 × 4 = 28), with 1 left over. Check it: 7 × 4 + 1 = 29. It gives back the number you started with, so it's right.
When the remainder is 0, the groups come out even, and b divides a exactly: 24 ÷ 6 has remainder 0.
6 rows of 4 and 4 rows of 6 are the same tray turned sideways: both 24.
Two shortcuts, and one thing you can't do. Multiplying by 10 shifts every digit one place to the left: 37 × 10 = 370, because 3 tens become 3 hundreds and 7 ones become 7 tens. Multiplying by 100 shifts two places: 37 × 100 = 3,700. That's place value again, and it's why our number system is so handy.
Dividing by 1 changes nothing: one group holds everything. But dividing by 0 has no answer. "How many groups of 0 make 12?" No number of empty groups ever reaches 12. Python agrees: it stops with a ZeroDivisionError rather than make up an answer.
In your own words
In your own words: how can you check that a division with a remainder is right?
Write a sentence or two, then compare it with a good answer in the lesson.
In Python: four signs for groups
Purpose. Python has a sign for each question you can ask about groups:
It runs right here in your browser, with nothing to install.
Now take the worked example from the math part, 29 ÷ 4, and step through it a line at a time. Watch what each line prints: // first, for the full groups, then %, for what's left over. The third line puts them back together, the check a = q × b + r, and the last asks / for the exact share.
Notice that / gives 4.6 and 7.25, decimals, while // gives 4 and 7, whole numbers. Python keeps two kinds of number. Whole numbers (like 4) are called int, short for integer. Decimals (like 4.6) are called float. Plain / always gives a float, even when the share comes out even:
print(24 / 6) # 4.0: still a decimalprint(24 // 6) # 4: a whole number
Worked example
Worked example: rebuilding a number from its groups
Split 1,000 into groups of 32, then put it back together to check the split is right.
print(1000 // 32) # full groupsprint(1000 % 32) # left overprint(31 * 32 + 8) # groups back together: should be 1000print(1000 / 32) # the exact share, as a decimal
In the lesson you can change this and run it.
1000 // 32 is 31 full groups and 1000 % 32 leaves 8. Putting them back, 31 × 32 + 8 = 1000, so the split is right: that's the formula a = q × b + r, checked by Python. The last line shows the exact share, 31.25. The .25 part is the 8 left over as a fraction of a group: 8 out of 32 is a quarter.
Quick check
What does this print?
print(17 // 5)
print(17 % 5)
print(17 / 5)
3
3
3.4
4
2
3.4
3
2
3.4
3.4
2
3
Show the answer
3
2
3.4
Three full 5s fit in 17 (15), leaving 2. The exact share is 3.4.
Mixing the two kinds. When a calculation mixes a whole number with a decimal, the answer is a decimal. Python keeps the more exact kind, so nothing gets lost:
print(3 + 0.5) # 3.5print(2 * 1.5) # 3.0: a decimal, though it's a whole amountprint(7 // 2, 7 % 2) # 3 1: both whole numbers
Most of the time the difference doesn't matter: 3.0 and 3 are the same amount. It matters when you need a count of whole things, like vans, bags or batches. Then use //, which never gives a fraction of a group.
Quick check
You want to know how many leftover signs don't fill a whole batch of 32. Which line do you write?
print(1000 * 32)
print(1000 / 32)
print(1000 // 32)
print(1000 % 32)
Show the answer
print(1000 % 32)
% gives the remainder: what's left after the full batches.
Drill
Split, then check
Print three lines for 100 ÷ 7: the number of full groups, the remainder, and the check (full groups times 7, plus the remainder), which should come back to 100.
print(100 / 7)
print(100 // 7)
print(0)
In the lesson, your code is checked as you write it.
In your own words
Why does 24 / 6 print 4.0 and not 4?
Write a sentence or two, then compare it with a good answer in the lesson.
In AI and ML: cutting data into batches
Purpose. A model doesn't learn from all its examples at once. It learns from a small batch, adjusts, then takes the next batch. One pass through every example is called an epoch.
So training starts with a division. With 1,000 trail signs and batches of 32:
full batches=examples//batch sizeleft over=examples%batch size
In plain words: the full batches are the quotient, and the leftover examples are the remainder.
print(1000 // 32) # full batchesprint(1000 % 32) # signs left over
The 8 left over don't vanish. Usually they make one last, smaller batch, so an epoch takes 31 + 1 = 32 steps. Training for 5 epochs is then 5 × 32 = 160 steps. Some setups drop the small last batch instead, which gives 31 steps an epoch.
Why batches at all? A model learns by looking at examples, measuring its mistakes and nudging its numbers. Looking at all 1,000 signs before every nudge is slow, and on a huge data set it may not even fit in the computer's memory. Looking at one sign at a time is fast but jumpy, because one sign can be unusual. A batch of 32 is the middle path: big enough to be steady, small enough to be quick. So the first thing any training run works out is how the examples divide into batches.
The flow below does it for the 1,000 signs. A square for every sign would be too many to draw, so its picture gives each batch one bar, as long as a full batch. Watch the last bar: the 8 leftover signs only partly fill it.
You change the batch size from 32 to 100 for the same 1,000 signs. What changes?
The number of signs changes
10 steps an epoch, nothing left over
100 steps an epoch
32 steps an epoch, 8 left over
Show the answer
10 steps an epoch, nothing left over
1,000 // 100 = 10 full batches, and 1,000 % 100 = 0, so there's no smaller last batch.
In the real tools. PyTorch, the library most models are built with, does this division for you. Its DataLoader takes the examples and a batch size, and hands the model one batch at a time. Here it is on 1,000 stand-in signs, made by torch.arange(1000):
Drill
Batches for a new data set
The club has 2,500 sign pictures and trains with batches of 64. Print three lines: the full batches, the signs left over, and the total steps for 4 epochs if the leftovers make one extra batch each epoch.
print(2500 / 64)
print(0)
print(4 * 39)
In the lesson, your code is checked as you write it.
In your own words
What does an epoch's number of steps depend on?
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