Module 1 · Section 3 of 10
Lesson 1.2 - Variables & Data Types
Target: ~9 min read - 20 min hands-on
Overview
Every calculation you automate starts with variables. Python infers the type from the
value you assign. Four types you'll use constantly: int (whole numbers), float
(decimals), str (text), bool (True/False).
Define constants once - R_gas = 8.314 - and reuse them, rather than hardcoding the
number everywhere. This lesson also covers unit conversions, which every discipline
runs into: hp and kW, bar and atm, kN and kip, rpm and rad/s, Mbps and MB/s.
Why This Matters (PH Context)
PH practice mixes unit systems constantly - SI in the codes, but imported equipment specs, ASHRAE and IEEE references, and older shop data routinely use imperial or metric-technical units. A small library of conversion variables becomes a reusable tool for the rest of your career, whatever field you're in.
Code-Along
# Lesson 1.2 - Variables, types, and unit conversions
import math # gives us math.pi, math.sqrt, trig functions, etc.
# --- Constants: name a value once, reuse it everywhere (update in one place) ---
g = 9.81 # m/s^2, gravitational acceleration
R_gas = 8.314 # J/(mol K), universal gas constant
e_charge = 1.602e-19 # C, elementary charge (1.602e-19 = 1.602 x 10^-19)
atm_kPa = 101.325 # kPa, standard atmosphere
# --- Python infers the TYPE from the value you assign ---
node_count = 24 # int - whole number [Computer]
supply_voltage_V = 230.0 # float - has a decimal point [Electrical]
process_fluid = "ethanol" # str - text, in quotes [Chemical]
within_spec = True # bool - True / False [Industrial]
print(type(node_count), type(supply_voltage_V), type(process_fluid), type(within_spec))
# --- Unit conversions are just multiplication by a known factor ---
# [Electrical] horsepower -> kilowatts (1 hp = 0.7457 kW)
p_hp = 20
p_kW = p_hp * 0.7457
print(f"{p_hp} hp = {p_kW:.2f} kW") # f-string: {expr:.2f} inserts a value, 2 dp
# [Chemical] gauge pressure -> absolute. x100: bar -> kPa; then add 1 atm
p_bar_g = 2.5
p_kPa_abs = p_bar_g * 100 + atm_kPa
print(f"{p_bar_g} bar(g) = {p_kPa_abs:.1f} kPa(a) = {p_kPa_abs / atm_kPa:.2f} atm")
# [Mechanical] revolutions/min -> radians/s (one rev = 2*pi rad, one min = 60 s)
rpm = 1750
omega = rpm * 2 * math.pi / 60
print(f"{rpm} rpm = {omega:.2f} rad/s")
# [Civil] kilonewtons -> kips (1 kN = 0.224809 kip)
kN = 50
kip = kN * 0.224809
print(f"{kN} kN = {kip:.2f} kip")
Expected output:
<class 'int'> <class 'float'> <class 'str'> <class 'bool'>
20 hp = 14.91 kW
2.5 bar(g) = 351.3 kPa(a) = 3.47 atm
1750 rpm = 183.26 rad/s
50 kN = 11.24 kip
Practice Exercises
- [Computer] A
4.7GB file is transferred over a120Mbps link. Compute the time in seconds and minutes. (1 GB = 8000 Mbit) - [Chemical] Convert a flow of
45L/s to m^3/s and m^3/day. - [Electrical] Given
power_kW = 3.5atvoltage_V = 230, compute the current in amperes and the energy in kWh used over 6 hours.
# Try the practice exercises here
Knowledge Check
- Which type would you use to store "Yes, this sample passed inspection"?
- What is the benefit of defining
R_gas = 8.314as a variable instead of typing the number everywhere? - What will
type(230.0)return?
Answer key
bool(as a True/False flag -stris also defensible for narrative text)- Easier to update in one place, and self-documenting
<class 'float'>
3 / 63 sections · Course home · Join the coaching cohort