Module 1 · Section 9 of 10
Lesson 1.8 - Engineering Calculations in Python
Target: ~10 min read - 25 min hands-on
Overview
This capstone lesson pulls everything together: variables, control flow, functions, data structures - applied to three calculations from different fields: an electrical feeder voltage drop, a chemical tank transfer, and a cross-discipline unit-conversion library. The goal isn't to teach the theory from scratch; it's to show how a formula wrapped in a well-named function becomes something you can trust, reuse, and build on. This is your bridge into Mini-Project 1.
Why This Matters (PH Context)
A tested, reusable function - whether it sizes a feeder, a transfer pump, or a batch schedule - lets you screen options quickly instead of running the formula by hand for each candidate, in any discipline.
Code-Along
# Lesson 1.8 - Putting it together
# Each block: wrap a formula in a well-named function, verify once, then reuse.
# --- 1. [Electrical] single-phase feeder voltage drop ---
def feeder_drop(current_A, length_m, resistance_ohm_per_km, source_V=230.0):
"""Two-way voltage drop (V), drop as % of source, and I^2R loss (W)."""
# /1000: ohm/km -> ohm/m; *2: current flows out AND back along the feeder
r_total = resistance_ohm_per_km / 1000 * length_m * 2
vdrop = current_A * r_total # Ohm's law
loss_W = current_A ** 2 * r_total # I^2 R, power lost as heat
return vdrop, vdrop / source_V * 100, loss_W # three values -> a tuple
vd, vd_pct, loss = feeder_drop(40, 75, 1.15) # unpack the returned tuple
print(f"Voltage drop: {vd:.2f} V ({vd_pct:.2f} %), line loss: {loss:.1f} W")
# --- 2. [Chemical] tank transfer time and batch mass ---
def transfer(volume_m3, flow_m3s, rho=998.0):
"""Return (seconds, minutes, transferred mass in kg)."""
seconds = volume_m3 / flow_m3s # time = volume / flow rate
return seconds, seconds / 60, volume_m3 * rho # mass = volume * density
s, mins, mass = transfer(12.0, 0.008)
print(f"Transfer: {s:.0f} s ({mins:.1f} min), batch mass: {mass:.0f} kg")
# --- 3. A cross-discipline unit-conversion library ---
# Tiny one-line functions - trivial alone, but reusable and self-documenting
def hp_to_kw(x): return x * 0.7457
def psi_to_kpa(x): return x * 6.89476
def kn_to_kip(x): return x * 0.224809
def lps_to_m3h(x): return x * 3.6
def c_to_k(x): return x + 273.15
# a dict of {label: computed value}, then print each pair
demo = {
"25 hp -> kW": hp_to_kw(25),
"150 psi -> kPa": psi_to_kpa(150),
"600 kN -> kip": kn_to_kip(600),
"45 L/s -> m3/h": lps_to_m3h(45),
}
print()
for label, value in demo.items():
print(f" {label:<18} = {value:.2f}") # {:<18} = left-align in an 18-char field
Expected output:
Voltage drop: 6.90 V (3.00 %), line loss: 276.0 W
Transfer: 1500 s (25.0 min), batch mass: 11976 kg
25 hp -> kW = 18.64
150 psi -> kPa = 1034.21
600 kN -> kip = 134.89
45 L/s -> m3/h = 162.00
Practice Exercises
- [Civil] Write
beam_load_check(w_kN_per_m, L_m, capacity_kN)returning the utilisation ratio (demand / capacity) and"OK"or"OVER". - [Computer] Add
bytes_to_gib(x)(divide by2**30) andmbps_to_MBps(x)(divide by 8) to the unit-conversion library. - [Electrical] Extend
feeder_dropto also return a boolean flag for whether the percentage drop exceeds a 3% limit.
# Try the practice exercises here
Knowledge Check
- In
feeder_drop, why is the resistance multiplied by 2? - Why is it useful to return a tuple of related results from
transferrather than printing inside it? - What does building a small "unit conversion library" of functions demonstrate?
Answer key
- Current flows out along one conductor and back along another - both contribute resistance
- The caller can use the numbers in further calculations, and the function stays testable
- Reusable code reduces repeated formula errors
9 / 63 sections · Course home · Join the coaching cohort