1.Why should I care?
The maths in a textbook uses layout to show order: a fraction bar, a raised power. Code is one flat line, so the order has to be spelled out with precedence and brackets. This is where engineering mathematics meets programming, and where many real calculation bugs start.
- ◇ Translate an engineering formula into a correct Python expression with named variables and units.
- ◇ Apply Python's operator precedence: brackets, **, unary minus, * / // %, then + −.
- ◇ Check an expression with a units check and an estimate.
2.Predict
With v_in = 5, r1 = 1000 and r2 = 2200, what does v_in * r2 / r1 + r2 give?
Guess first. Being wrong here is how the surprise works.
3.Discover
Pick 'Voltage divider' and step through each expression: in which step does the wrong one go astray? Then try 'Kinetic energy' (is 1 / 2 * m * v ** 2 right?) and 'Average speed' (why is averaging the two speeds wrong?).
Loading the interactive…
Can't use the interactive? Read a description instead
The precedence explorer shows a formula, sliders for its inputs, and several Python expressions. For the chosen expression it lists every evaluation step. Example, voltage divider with 5 V, 1000 Ω, 2200 Ω: v_in * r2 / (r1 + r2) → 5 * 2200 / 3200 → 11000 / 3200 → 3.4375 (matches); v_in * r2 / r1 + r2 → 11000 / 1000 + 2200 → 11.0 + 2200 → 2211.0 (does not match).
4.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
Which Python line correctly computes V_out = V_in·R2/(R1 + R2)?
Practice 2
What does Python give for -3 ** 2?
5.Understand
Python's order of operations
- Brackets. 2. Powers
**(worked right to left). 3. Unary minus, so-3 ** 2is-9. 4.*,/,//,%from left to right. 5.+,-from left to right. When in doubt, add brackets: they cost nothing and show your intent.
A translation recipe
- Name each quantity with its unit:
mass_kg,speed_m_per_s. 2. Replace a fraction bar with/and wrap the whole top and bottom in brackets. 3. Write powers with**. 4. Run it with numbers you can check by hand.
mass_kg = 1.2
speed_m_per_s = 3
energy_j = 0.5 * mass_kg * speed_m_per_s ** 2
print(round(energy_j, 2)) # 5.4Units check and estimate
For E = ½mv²: kg × (m/s)² = kg·m²/s² = J, so the formula is consistent. Estimate: ½ × 1 × 3² ≈ 4.5, close to 5.4. If an answer is out by a factor of 1000 or has the wrong unit, a conversion or bracket is missing.
Explain it another way
A formula on paper uses its shape (a fraction bar, a small raised 2) to show what happens first. Code has no shape, so brackets do that job.
Python parses an expression into a tree using precedence and associativity, then evaluates it depth-first, left operand before right. is right-associative: 2 3 2 = 2 9 = 512.
V_out = V_in·R₂/(R₁ + R₂). With 5 V, 1 kΩ and 2.2 kΩ: 5 × 2200 / 3200 = 3.4375 V. Check the limits: if R₂ → 0, V_out → 0; if R₁ → 0, V_out → V_in. Both are physically right.
Voltage dividers scale a battery voltage down so a microcontroller can measure it. A bracket slip in the scaling code makes the robot think its battery is full when it is nearly flat.
Derivation: Average speed is total distance over total time
A robot drives d₁ = 120 m in t₁ = 40 s, then d₂ = 80 m in t₂ = 60 s.
Average speed means one steady speed that covers the same total distance in the same total time: v = (d₁ + d₂) / (t₁ + t₂).
Substitute: v = (120 + 80) / (40 + 60) = 200 m / 100 s = 2.0 m/s.
Averaging the two speeds gives (3.0 + 1.33) / 2 ≈ 2.17 m/s, which is wrong because the robot spends longer on the slow leg.
In Python the brackets carry the fraction bar:
(d1_m + d2_m) / (t1_s + t2_s). Without them,d1_m + d2_m / t1_s + t2_sgives 120 + 2 + 60 = 182.0, with meaningless units.
Worked example 1
Write F = C × 9/5 + 32 in Python and check it at 100 °C.
temp_f = temp_c * 9 / 5 + 32. Left to right: 100 × 9 = 900, / 5 = 180.0, + 32 = 212.0 °F, which is right for boiling water. temp_c * 9 / (5 + 32) would divide by 37 instead and give about 24.3.
Common mix-ups
- Python works out a line from left to right, whatever the operators.
✓ Precedence comes first: ** before * and /, which come before + and −. Left to right only applies within the same level. - Extra brackets are bad style.
✓ Brackets that show intent are good style in engineering code, especially around a whole numerator or denominator.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 3 · a fresh version every time
A 2 kg robot moves at 36 km/h. Convert to m/s (divide by 3.6), then use E = 0.5 * m * v ** 2. What is its kinetic energy, in joules?
Practice 4
Is 1 / 2 * m * v ** 2 a correct translation of ½mv² in Python 3?
Practice 5
A robot drives 300 m in 100 s, then 100 m in 100 s. What is its average speed, in m/s?
Practice 6
Someone writes power_w = voltage_v / current_a. What does a units check say?
7.Apply it
Translate three formulas you know (for example density = mass/volume, P = V·I, and the voltage divider) into Python with named variables and units. For each, write one test value you can check by hand and run it.
8.Reflect
9.Go further
- Python reference: operator precedence ↗The official table of which operators bind tighter.
Concepts: Engineering formulas as code · Variables, types and expressions