Index Ai Academy
Phase 0 · ToolsNumbers for code

Formulas: order, substitution and rearranging

About 20 minLooking ahead: some prerequisites aren't done yet

Why this 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.

  • 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
Quick check
Before any explaining, a guess: what does this print?
print(10 - 4 / 2 * 3)

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

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.