Lesson 04: Numbers, Arithmetic Operators, Modulo Batching & Scientific Notation
The “Boring Stuff” Problem: Subtle Number Bugs That Break Chemistry & Hydrology Code
Computers store whole numbers (int) and decimal numbers
(float) differently. Most beginner bugs happen not because
Python can’t do calculus, but because of four arithmetic surprises:
| Operator | Meaning | Example | Environmental Use Case |
|---|---|---|---|
/ |
True float division | 7 / 2 -> 3.5 |
Concentrations, Reynolds number, HRT |
// |
Floor (integer) division | 137 // 24 -> 5 |
Full sample bottle coolers or filter beds |
% |
Remainder (modulo) | 137 % 24 -> 17 |
Leftover grab bottles or leap-hour checks |
** |
Exponentiation | d ** 2 |
Circular pipe area, \(T^4\) radiation, decay |
Watch Out! Two Numeric Traps
- Binary Float Precision
(
0.1 + 0.2 == 0.30000000000000004): NEVER test floats withabs_diff == 0.3! Usemath.isclose(a, b, rel_tol=1e-9)orabs(a - b) < 1e-9. - Never Write
math.log(H)for pH:math.logis natural logarithm (\(\ln\)). Always writemath.log10for \(\text{pH} = -\log_{10}[\text{H}^+]\)!
import math
# Example 1 (Field Sampling & Chlorine Contact Disinfection): Coolers + CT Value + Coliform Log-Reduction
total_grab_vials = 137
bottles_per_cooler = 24
full_coolers = total_grab_vials // bottles_per_cooler
partial_cooler_bottles = total_grab_vials % bottles_per_cooler
# Chick-Watson Disinfection Kinetic Model: N(t) = N0 * exp(-k_cw * C^n * t)
N0_cfu_100mL = 2.5e5 # Raw river coliform count [CFU/100mL]
C_cl2_mgL = 1.8 # Free chlorine residual [mg/L]
t10_min = 32.0 # Baffled contact basin T10 detention [min]
k_inact = 0.24 # L/(mg*min) inactivation constant
n_dil = 1.0
N_out = N0_cfu_100mL * math.exp(-k_inact * (C_cl2_mgL ** n_dil) * t10_min)
log_reduction = math.log10(N0_cfu_100mL / N_out)
print("=== Field Sampling & Disinfection Kinetics ===")
print(f"Cooler Packing : {full_coolers} full coolers + {partial_cooler_bottles} bottles in cooler #{full_coolers + 1}")
print(f"Chlorine CT : {C_cl2_mgL * t10_min:.1f} mg*min/L")
print(f"Effluent Count : {N_out:.2f} CFU/100mL ({log_reduction:.2f}-log inactivation)")=== Field Sampling & Disinfection Kinetics ===
Cooler Packing : 5 full coolers + 17 bottles in cooler #6
Chlorine CT : 57.6 mg*min/L
Effluent Count : 0.25 CFU/100mL (6.00-log inactivation)
# Example 2 (Planetary Energy Balance & Heat Transfer Course): Earth Radiative Equilibrium Temperature
# Compare blackbody Earth (no atmosphere) vs greenhouse radiative equilibrium using sigma * T^4
sigma_sb = 5.670374e-8 # Stefan-Boltzmann constant [W/(m^2*K^4)]
S0_solar = 1361.0 # Solar constant at 1 AU [W/m^2]
albedo_alpha = 0.30 # Planetary Bond albedo [-] (30% reflected)
# Quarter-sphere geometric projection factor (pi R^2 / 4 pi R^2 = 1/4)
absorbed_flux = (S0_solar / 4.0) * (1.0 - albedo_alpha)
T_eff_K = (absorbed_flux / sigma_sb) ** 0.25
T_eff_C = T_eff_K - 273.15
print("=== Planetary Radiative Equilibrium (HMT + Climate Science) ===")
print(f"Absorbed Solar Flux : {absorbed_flux:.2f} W/m^2")
print(f"Effective Temp (no GHG): {T_eff_K:.2f} K ({T_eff_C:.2f} degC)")
print(f"Actual Earth Avg (~288K): Greenhouse Effect = +{288.0 - T_eff_K:.1f} K warming!")=== Planetary Radiative Equilibrium (HMT + Climate Science) ===
Absorbed Solar Flux : 238.17 W/m^2
Effective Temp (no GHG): 254.58 K (-18.57 degC)
Actual Earth Avg (~288K): Greenhouse Effect = +33.4 K warming!
Concept Check Questions
- What is the difference between
17 / 5,17 // 5, and17 % 5in Python? - Why did we divide
S0_solarby4.0when computing planetary average incoming irradiance? - What is the scientific-notation literal in Python for Avogadro’s constant (\(6.022\times 10^{23}\)) and \(2.3\times 10^{-5}\)?
- Why is
abs(c1 - c2) < 1e-9safer thanc1 == c2in chemical equilibrium loops?
Guided Practice Problem 1: Albedo Land-Use Feedback (LCA & Climate)
If large-scale deforestation lowers regional surface albedo from \(\alpha = 0.30\) to \(\alpha = 0.26\), compute the increase in absorbed solar flux (\(\Delta F\), \(\text{W/m}^2\)) and the resulting rise in effective radiative temperature \(\Delta T_{\text{eff}}\) (\(\text{K}\)).
# Guided Practice 1 Starter
S0 = 1361.0
sigma = 5.670374e-8
flux_base = (S0 / 4.0) * (1.0 - 0.30)
flux_defor = (S0 / 4.0) * (1.0 - 0.26)
T_base = (flux_base / sigma) ** 0.25
T_defor = (flux_defor / sigma) ** 0.25
print(f"dFlux = +{flux_defor - flux_base:.2f} W/m^2 | dT_eff = +{T_defor - T_base:.2f} K")dFlux = +13.61 W/m^2 | dT_eff = +3.56 K