Index Ai Academy
Phase 0 · Tools · Numbers for code

Formulas: order, substitution and rearranging

Work a calculation in the right order, fill in a formula, and turn it around to find what's missing.

About 20 minutes · Free lesson

Start this lesson

Why it matters

Every model, metric and price in this course is a formula. Cost is tokens times a price, accuracy is correct answers over all answers, and a neuron computes y = w × x + b. Reading a formula takes three moves: knowing which part is worked out first, putting numbers in for the letters, and turning it around when the number you need is on the wrong side. Those three moves are most of the algebra you'll need, and Python checks each one for you.

What you'll be able to do

  • Work out a calculation in the right order: brackets, powers, then × and ÷, then + and −
  • Substitute numbers for the letters in a formula and compute the result
  • Rearrange a formula to find the number you're missing
  • Check a formula with units and an easy test value

What it covers

  1. Which part comes first
  2. Letters stand for numbers
  3. Price an API call
  4. From paper to code
  5. Rearranging: finding the missing number
  6. When does the stall break even?
  7. Checking a formula
  8. Formulas in AI
Quick check
Before any explaining, a guess: what does this print?
print(10 - 4 / 2 * 3)
  1. 1.0
  2. 4.0
  3. 9.333333333333334
  4. 9.0
Show the answer

4.0

Division and multiplication come before subtraction, and they run left to right: 4 / 2 is 2.0, times 3 is 6.0, and 10 − 6.0 is 4.0.

Which part comes first

A calculation with several operators needs an agreed order, or two people get two answers. Python uses the same order as school math:

  1. Brackets first.
  2. Then powers (**).
  3. Then multiplying and dividing (*, /, //, %), left to right.
  4. Then adding and subtracting, left to right.
print(2 + 3 * 4)        # 3 * 4 first: 14
print((2 + 3) * 4)      # brackets first: 20
print(2 * 3 ** 2)       # the power first: 18
print(-3 ** 2)          # the power before the minus sign: -9
It runs right here in your browser, with nothing to install.

The last line catches nearly everyone: -3 ** 2 is −(3²), not (−3)². When in doubt, add brackets. They cost nothing, and they show the next reader exactly what you meant.

"Left to right" matters more than it looks. 8 / 4 / 2 is (8 / 4) / 2 = 1.0, not 8 / (4 / 2) = 4.0. Operators of the same rank are worked out in the order you read them, so if you mean the other grouping, you need brackets.

Try it now

Each line uses the one before it, so the order is forced: you can't take 10% off a subtotal that doesn't exist yet.

Letters stand for numbers

A formula uses letters for the numbers that change. "fare = 3.50 + 2.25 × miles" works for any trip: put the miles in, and out comes the fare. Putting a number in for a letter is called substituting, and Python variables are exactly this:

miles = 8
fare = 3.50 + 2.25 * miles
print(fare)

The order rules still apply after substituting: here the multiplication happens before the addition, so the 2.25 is charged per mile and the 3.50 only once. Many formulas give their letters short names, like w for a weight or n for a count. They're still just numbers waiting to be filled in.

Quick check
A taxi charges a fixed 3.50 plus 2.25 per mile. What does this print?
miles = 8
fare = 3.50 + 2.25 * miles
print(fare)
  1. 13.75
  2. 21
  3. 21.5
  4. 46.0
Show the answer

21.5

The multiplication comes first: 2.25 × 8 = 18.0. Then 3.50 + 18.0 = 21.5.

Drill

Price an API call

A model charges $3 per million input tokens and $15 per million output tokens. A call used 12,000 input tokens and 800 output tokens. Set cost to the call's price in dollars, rounded to 4 decimal places, using the formula: input_tokens ÷ 1e6 × input_price + output_tokens ÷ 1e6 × output_price.

input_tokens = 12000
output_tokens = 800
input_price = 3      # dollars per million input tokens
output_price = 15    # dollars per million output tokens

cost = 0

print("This call cost $", cost)
In the lesson, your code is checked as you write it.

From paper to code

Formulas on paper leave out things that code has to spell out:

  • 2x means 2 × x, written 2 * x in Python, and xy means x × y.
  • A small raised number is a power: x² is x ** 2.
  • A dot, ·, or a cross, ×, means multiply; ÷ and / both mean divide.
  • A fraction bar groups everything above it and everything below it, as if each had brackets: (a + b) over 2 is (a + b) / 2 in code, never a + b / 2.

Copying a formula from a page into code is where most of these mistakes creep in, and Python won't warn you, because the wrong version is still valid code, so read each part of the formula aloud and check the brackets in your code match it.

Quick check
A formula on paper shows a + b on top of a fraction bar, with 2 underneath: the average of a and b. Which line of Python matches it?
  1. a + b / 2
  2. (a + b) / 2
  3. a / 2 + b
  4. a + (b / 2)
Show the answer

(a + b) / 2

The fraction bar groups the whole top, so the sum has to be in brackets: (a + b) / 2. Without them, only b is divided by 2.

Rearranging: finding the missing number

Sometimes you know the answer and need one of the inputs. A ride cost $21.50; how many miles was it? The formula gives the fare from the miles, so you turn it around. The rule: do the same thing to both sides, undoing the steps in reverse order.

The fare was worked out as "times 2.25, then add 3.50". To undo it, subtract 3.50 first, then divide by 2.25:

fare = 21.50
miles = (fare - 3.50) / 2.25
print(miles)
print(3.50 + 2.25 * miles)    # substitute back: the fare again

Substituting the answer back into the original formula is the best check there is: if you get the number you started with, the rearranging was right. The same steps turn y = 2x + 3 into x = (y − 3) / 2.

Percentages are where rearranging goes wrong most often. A jacket costs $48 after 20% off; what was the full price? The sale price was full × 0.8, so the full price is 48 ÷ 0.8 = $60. Adding 20% back, 48 × 1.2, gives $57.60, which is wrong: 20% of the smaller sale price is less than 20% of the original. Undo a multiplication by dividing.

Quick check
The taxi formula is fare = 3.50 + 2.25 × miles. A ride cost 21.50. Which line finds the miles?
  1. miles = 21.50 / 2.25 - 3.50
  2. miles = (21.50 - 3.50) / 2.25
  3. miles = (21.50 + 3.50) * 2.25
  4. miles = 21.50 - 3.50 / 2.25
Show the answer

miles = (21.50 - 3.50) / 2.25

Undo the steps in reverse order: take away the fixed 3.50 first, then divide by the per-mile 2.25. (21.50 − 3.50) / 2.25 = 8 miles.

Drill

When does the stall break even?

A market stall sells hats at $25 each. Each hat costs $10 to make, and the stall costs $3,000 to set up. Profit = sales × (price − cost) − setup. Set profit_at_150 to the profit from 150 hats, and break_even to the number of hats where profit is exactly 0 (rearrange the formula).

price = 25
cost = 10
setup = 3000

profit_at_150 = 0
break_even = 0

print(profit_at_150, break_even)
In the lesson, your code is checked as you write it.

Checking a formula

Three quick habits catch most formula mistakes:

  • Units. Dollars per mile times miles gives dollars; if your units come out as "dollars squared", something is multiplied that shouldn't be.
  • An easy value. Put in a number you can work out in your head. A 0-mile taxi ride should cost the fixed 3.50, and a 1-mile ride should cost 3.50 + 2.25 = 5.75. If either comes out differently, you've found the bug.
  • A known case. If you know one real answer, like last month's bill, check that the formula reproduces it exactly, to the cent.

These checks take seconds, and they're the same ones you'll use on a model's loss function or a cost estimate in later phases. A formula that passes all three isn't guaranteed to be right, but one that fails any of them is certainly wrong, and finding that out early is cheap.

Quick check
The stall from the drill sold only 150 hats. As in the negative-numbers lesson, what does the sign of this result tell you?
sales = 150
profit = sales * (25 - 10) - 3000
print(profit)
  1. 750
  2. -2250
  3. 2250
  4. -750
Show the answer

-750

150 × 15 = 2,250, minus the 3,000 setup is −750. The minus sign says it's a loss: the stall hasn't yet earned back its setup cost.

Formulas in AI

The calculator above is a neuron: y = w × x + b, with a weight and a bias that a model learns from data. A model's loss is a formula that scores how wrong its predictions are. The cost of an API call is a formula of tokens and prices. Even the laws that predict how good a model will get from more data and compute are formulas, with powers in them.

In Phase 2 you'll learn to read math written in symbols, like Σ for "add these up". It's the same three moves: know the order, substitute, and rearrange when you need to. Papers and documentation write formulas in symbols, and your code writes them in Python; moving between the two, carefully, is one of the most useful skills in AI engineering.

Quick check
In y = w × x + b, which numbers does a model learn from data?
  1. Nothing: the formula is fixed
  2. x, the input it's given
  3. y, which it's told in advance
  4. w and b, the weight and the bias
Show the answer

w and b, the weight and the bias

The input x comes with each example, and y is the prediction. Training adjusts w and b until the predictions match the data well.

Quick check
What does this print?
x = -3
print(x ** 2, -3 ** 2)
  1. -9 -9
  2. 9 9
  3. -9 9
  4. 9 -9
Show the answer

9 -9

With a variable, x ** 2 squares −3 to give 9. Written out, -3 ** 2 is −(3²) = −9, because ** comes before the minus sign. (-3) ** 2 would give 9.

Practice what you just read

  • 3 code examples you can edit and run
  • a practice exercise that checks your code
  • a 6-question quiz
  • 5 review cards for spaced repetition

Start this lesson

Try a quiz question

Total = 5 + 3 × n. The total was 29. Which line finds n?

  1. n = (29 - 5) / 3
  2. n = 29 / 3 - 5
  3. n = (29 + 5) * 3
  4. n = 29 - 5 / 3

Answer it at the end of the lesson to see if you're right, and why.

Before this lesson