Lite / Building Machines That Respond / Week 4
Translate a formula into Python
Two programs compute a voltage divider's output. One says 3.4 V, the other 2211 V. Which pair of brackets is missing?
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.
Predict
With v_in = 5, r1 = 1000 and r2 = 2200, what does v_in * r2 / r1 + r2 give?
- 3.4375
- 2211.0
- An error
Guess first, then reveal
2211.0. Division happens before addition, so Python computes (5 × 2200 / 1000) + 2200 = 11.0 + 2200. The formula needs v_in * r2 / (r1 + r2) = 3.4375 V.
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?).
What the interactive shows (text version)
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).
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
Intuitive
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.
Technical
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.
Mathematical
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.
Applied
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.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
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.
Reflect
Which precedence rule surprised you most, and how will you protect yourself from it?
Write your answer on paper or in your own notes. Lite never stores what you write.
Go further
- Python reference: operator precedence
The official table of which operators bind tighter.