Lesson
07: Conditionals (if-elif-else) — Automating EPA Compliance
& Hydraulic Safety Gates
The “Boring Stuff” Problem: Checking 500 Daily Plant Logs Against Permit Rules
Manual compliance auditing looks like this: stare at row 1, check if
BOD_out <= 30, check if
6.0 <= pH <= 9.0, check if
SS_removal >= 85%, highlight red in Excel—repeat 500
times. By row 40, human fatigue misses violations.
Think of if / elif / else like an automated
sorting gate on a sensor pipeline:
- Order checks from most specific/strict to
broadest, because Python enters the
FIRST
Truebranch and skips all remainingelifbranches! - Use chained Python comparisons cleanly:
6.0 <= ph <= 9.0reads directly like mathematical notation. - Combine multi-condition rules using
and(both must hold),or(either triggers alarm), andnot.
# Example 1 (EPA Clean Air Act): Piecewise Linear PM2.5 Air Quality Index (AQI) Classifier
def classify_pm25_aqi(pm25_ug_m3):
# US EPA 24-h PM2.5 Breakpoints: (C_lo, C_hi, I_lo, I_hi, Category)
if pm25_ug_m3 <= 9.0:
c_lo, c_hi, i_lo, i_hi, cat = 0.0, 9.0, 0, 50, "Good"
elif pm25_ug_m3 <= 35.4:
c_lo, c_hi, i_lo, i_hi, cat = 9.1, 35.4, 51, 100, "Moderate"
elif pm25_ug_m3 <= 55.4:
c_lo, c_hi, i_lo, i_hi, cat = 35.5, 55.4, 101, 150, "Unhealthy for Sensitive Groups"
elif pm25_ug_m3 <= 125.4:
c_lo, c_hi, i_lo, i_hi, cat = 55.5, 125.4, 151, 200, "Unhealthy"
elif pm25_ug_m3 <= 225.4:
c_lo, c_hi, i_lo, i_hi, cat = 125.5, 225.4, 201, 300, "Very Unhealthy"
else:
c_lo, c_hi, i_lo, i_hi, cat = 225.5, 325.4, 301, 500, "Hazardous"
aqi = round(((i_hi - i_lo) / (c_hi - c_lo)) * (pm25_ug_m3 - c_lo) + i_lo)
return aqi, cat
for sample_pm in [7.2, 24.0, 42.8, 96.5, 185.0]:
idx, label = classify_pm25_aqi(sample_pm)
print(f"PM2.5 = {sample_pm:6.1f} ug/m^3 -> AQI = {idx:3d} ({label})")PM2.5 = 7.2 ug/m^3 -> AQI = 40 (Good)
PM2.5 = 24.0 ug/m^3 -> AQI = 79 (Moderate)
PM2.5 = 42.8 ug/m^3 -> AQI = 119 (Unhealthy for Sensitive Groups)
PM2.5 = 96.5 ug/m^3 -> AQI = 180 (Unhealthy)
PM2.5 = 185.0 ug/m^3 -> AQI = 260 (Very Unhealthy)
from pathlib import Path
def data_file(name):
"""Locate a dataset file whether running from SRC_DIR, module folder, HOMEWORK, or EXAMS."""
for p in [
Path("datasets") / name,
Path("../datasets") / name,
Path("../../datasets") / name,
]:
if p.exists():
return p
raise FileNotFoundError(f"Missing dataset {name} in datasets/")
# Example 2 (Wastewater NPDES + Fluid Mechanics Pump Cavitation Gate): Multi-Criteria Decision Logic
# Load 3 real operating flows from Fluids_Pump_and_Pipe_Network.csv
pump_rows = data_file("Fluids_Pump_and_Pipe_Network.csv").read_text(encoding="utf-8").splitlines()[1:]
print("Flow [L/s] | H_sys [m] | H_pump [m] | NPSHa [m] | NPSHr [m] | Engineering Status")
print("-" * 82)
for row in pump_rows:
cols = row.split(",")
q_ls = float(cols[0])
h_sys, h_pump, eta = float(cols[3]), float(cols[4]), float(cols[5])
npsha, npshr = float(cols[7]), float(cols[8])
if npsha < 1.25 * npshr:
gate = "CAVITATION RISK (NPSHa < 1.25*NPSHr)"
elif h_pump < h_sys:
gate = "INSUFFICIENT PUMP HEAD"
elif eta < 0.60:
gate = "LOW EFFICIENCY OPERATING ZONE"
else:
gate = "SAFE & EFFICIENT"
print(f"{q_ls:10.1f} | {h_sys:9.2f} | {h_pump:10.2f} | {npsha:9.2f} | {npshr:9.2f} | {gate}")Flow [L/s] | H_sys [m] | H_pump [m] | NPSHa [m] | NPSHr [m] | Engineering Status
----------------------------------------------------------------------------------
20.0 | 23.86 | 51.26 | 6.70 | 2.28 | LOW EFFICIENCY OPERATING ZONE
40.0 | 29.04 | 49.04 | 6.09 | 2.54 | LOW EFFICIENCY OPERATING ZONE
60.0 | 37.46 | 45.34 | 5.09 | 2.96 | SAFE & EFFICIENT
80.0 | 49.12 | 40.16 | 3.69 | 3.54 | CAVITATION RISK (NPSHa < 1.25*NPSHr)
100.0 | 64.01 | 33.50 | 1.91 | 4.30 | CAVITATION RISK (NPSHa < 1.25*NPSHr)
110.0 | 72.67 | 29.62 | 0.87 | 4.74 | CAVITATION RISK (NPSHa < 1.25*NPSHr)
120.0 | 82.13 | 25.36 | -0.27 | 5.22 | CAVITATION RISK (NPSHa < 1.25*NPSHr)
130.0 | 92.40 | 20.73 | -1.50 | 5.75 | CAVITATION RISK (NPSHa < 1.25*NPSHr)
140.0 | 103.48 | 15.74 | -2.83 | 6.32 | CAVITATION RISK (NPSHa < 1.25*NPSHr)
150.0 | 115.37 | 10.38 | -4.26 | 6.92 | CAVITATION RISK (NPSHa < 1.25*NPSHr)
Concept Check Questions
- Look closely at Example 2: if a pump point has BOTH
npsha < 1.25 * npshrANDh_pump < h_sys, why does Python print"CAVITATION RISK"instead of"INSUFFICIENT PUMP HEAD"? - Write a single chained Python comparison checking that wastewater
effluent pH sits safely between
6.0and9.0. - Why must string comparisons like
"?"or"NA"happen BEFORE callingfloat(token)on a sensor reading?
Guided Practice Problem 1: EPA Secondary Wastewater NPDES Permit Classifier
For each sample
(day, bod_in, bod_out, ss_out, ph_out):
- Compute BOD removal efficiency
%. - A plant PASSES NPDES secondary effluent permit ONLY
if:
bod_out <= 30.0,ss_out <= 30.0,6.0 <= ph_out <= 9.0, ANDbod_removal_pct >= 85.0. Test on[("Mon", 210, 18, 22, 7.2), ("Tue", 180, 29, 25, 7.4), ("Wed", 240, 34, 28, 7.1), ("Thu", 195, 22, 19, 5.7)].
# Guided Practice 1 Starter
samples = [("Mon", 210, 18, 22, 7.2), ("Tue", 180, 29, 25, 7.4), ("Wed", 240, 34, 28, 7.1), ("Thu", 195, 22, 19, 5.7)]
for day, bin_, bout, ss, ph in samples:
rem = (bin_ - bout) / bin_ * 100.0
ok = (bout <= 30.0) and (ss <= 30.0) and (6.0 <= ph <= 9.0) and (rem >= 85.0)
print(f"{day}: BOD_out={bout} mg/L ({rem:.1f}% rem), SS={ss}, pH={ph} -> {'PASS' if ok else 'VIOLATION'}")Mon: BOD_out=18 mg/L (91.4% rem), SS=22, pH=7.2 -> PASS
Tue: BOD_out=29 mg/L (83.9% rem), SS=25, pH=7.4 -> VIOLATION
Wed: BOD_out=34 mg/L (85.8% rem), SS=28, pH=7.1 -> VIOLATION
Thu: BOD_out=22 mg/L (88.7% rem), SS=19, pH=5.7 -> VIOLATION