Skip to main content
Back to module
Foundations for Engineering & Energy Careers · Engineering Tools

Engineering calculations with Python

Reading1h 1mLesson 1 of 3 in this module

What you'll learn

  • Write a calculation with units and a sanity check
  • Put the unit in every variable and function name
  • Keep one formula per function and define constants once
  • Use assert to catch unit errors automatically

Write a small engineering calculation with units in every name and a built-in sanity check.

Why it matters

Engineers calculate constantly: loads, flows, currents, temperatures, costs. For decades those calculations lived on paper pads and in spreadsheets. Today many engineers also use short Python scripts, because a script is easy to check, easy to rerun when an input changes, and easy to hand to a colleague. You do not need to be a software developer; you need a few habits that make calculations trustworthy.

The core idea: a calculation is a mini design document

A good engineering calculation, on paper or in code, always shows:

  1. Inputs with units and where they came from.
  2. Assumptions stated plainly ("water at 20 C, density 1,000 kg/m^3").
  3. Formulas with a reference (textbook, code section, datasheet).
  4. Results with units.
  5. A sanity check: an independent estimate that shows the result is reasonable.

Code makes points 1, 3 and 4 explicit by its structure. Points 2 and 5 are up to you.

Habit 1: put the unit in the name

Python numbers carry no units, so length = 3 could be metres, feet or millimetres. Put the unit in the variable or function name instead:

Python
span_m = 6.0           # beam span in metres
load_kn_per_m = 4.5    # distributed load in kN per metre
total_load_kn = span_m * load_kn_per_m
print(total_load_kn)   # 27.0 kN

When you read span_m * load_kn_per_m, the units visibly multiply to kN. If you ever wrote span_ft * load_kn_per_m, the mismatch would jump out. (There are libraries, such as pint, that attach real units to numbers. They are worth learning later; naming is the habit that works everywhere.)

Habit 2: one formula per function

Small functions with clear names are easy to check against the textbook:

Python
G = 9.81  # m/s^2

def weight_kn(mass_kg):
    # Weight in kN from mass in kg: W = m * g, newtons to kN
    return mass_kg * G / 1000.0

A reviewer can confirm W = m x g and the newton-to-kilonewton conversion in five seconds.

Habit 3: build in a sanity check

Before trusting a result, compare it with something you know. One cubic metre of water has a mass of about 1,000 kg, so it weighs about 9.81 kN (roughly one tonne-force). If your tank function says a 1 m cube of water weighs 9,810 kN or 0.0098 kN, you have a unit error. You can make the check automatic with assert:

Python
assert abs(weight_kn(1000) - 9.81) < 0.01, "1,000 kg should weigh about 9.81 kN"

If the assertion fails, Python stops with your message, so a broken calculation cannot quietly produce a wrong number.

A worked example: will the floor hold the tank?

A 1.2 m x 0.8 m x 1.0 m deep water tank will sit on a floor. Is the average pressure within 4.8 kPa? (That is 100 psf, a typical code design live load for corridors and assembly areas; a real check uses the governing code and a structural engineer.)

Volume  = 1.2 x 0.8 x 1.0      = 0.96 m^3
Mass    = 0.96 x 1,000         = 960 kg
Weight  = 960 x 9.81 / 1,000   = 9.42 kN
Pressure= 9.42 kN / 0.96 m^2   = 9.81 kPa

9.81 kPa is about double 4.8 kPa, so the answer is no: the tank needs a stronger floor, a spreader platform designed by an engineer, or a different location. Notice that the pressure under a full-depth tank equals the water pressure at the bottom (rho x g x h = 9.81 kPa for 1 m), which is a second, independent sanity check.

This example is a teaching simplification. Real floor checks include the tank's own weight, concentrated loads at its feet, the floor's structure and the governing building code, and they are done or reviewed by a qualified engineer.

Common mistakes

  • Mixing kN and N, or mm and m. Factors of 1,000 are the classic engineering error.
  • Hard-coding numbers in many places. Define constants like G once.
  • No sanity check. A program that runs without errors can still be wrong.
  • Hidden assumptions. If the density or code value is assumed, write it in a comment.

Quick recap

  • Treat a calculation as a document: inputs, assumptions, formulas, results and a check.
  • Put units in names, keep one formula per function, and define constants once.
  • Use an independent estimate (and assert) to catch unit errors.

Try it

Open the Do tab. The script already runs; you will write the one formula that turns the tank's volume into a load in kN, and the checks will confirm it against the sanity check.

Key terms (5)

Assumptions
Values or conditions you take as given, such as water density, which a calculation should state plainly.
sanity check
An independent estimate that shows a result is reasonable, such as 1 m^3 of water weighing about 9.81 kN.
assert
A Python statement that stops the program with a message if a condition is false.
constants
Fixed values such as G that are defined once and reused instead of hard-coding the number in many places.
kN
Kilonewtons; 1 kN is 1,000 newtons, and mixing kN with N is a classic factor-of-1,000 error.

Recap

  • Treat a calculation as a document: inputs, assumptions, formulas, results and a check.
  • Python numbers carry no units, so put the unit in each name.
  • Keep one formula per function and define constants like G once.
  • Use an independent estimate and assert to catch unit errors such as kN versus N.
  • Teaching examples are simplified; real structural checks need a qualified engineer.
Also in this lesson: