💻 Module: 00-Introduction-Setup • Unabridged Executed Lesson
📑 Download Module Slides (PDF) 🐍 Download Notebook (.ipynb)

Lesson 01: Welcome to Python — Automating the Boring Engineering Stuff

Why Learn Python When You Already Have a Calculator and Excel?

Imagine your Fluids professor asks you to calculate the Darcy friction factor \(f\) and pressure drop \(\Delta P\) for 40 different pipe flow rates, or your Thermodynamics II team has to check turbine efficiency across 120 operating hours from a power plant telemetry sheet. On a handheld calculator, typing the same equation 40 times guarantees at least three typo mistakes. In a giant spreadsheet, one dragged cell reference (B4 turning into B5 by accident) silently ruins the entire table—and finding that bug takes hours.

Python solves this. Instead of doing repetitive, error-prone arithmetic by hand, you write a tiny step-by-step recipe once, double-check the physics on one hand-verified test case, and let Python run it across 10, 1,000, or 1,000,000 cases in less than a second—with printed units, clean formatting, and zero copy-paste accidents.

In this course, every Python idea you learn connects straight to your Mechanical Engineering courses on this portal:


How Python Thinks: The Mental Model

Think of Python as an ultra-fast, completely literal junior lab assistant sitting inside a Jupyter Notebook cell:

  1. Top-to-Bottom Execution: Python reads one line at a time from top to bottom. It never skips ahead unless a control statement tells it to.
  2. Memory Labels (Variables): When you write mass_kg = 12.5, Python creates the floating-point number 12.5 in memory and sticks a label named mass_kg onto it.
  3. Expressions Reduce to One Value: When Python sees 101.325 + 2.5 * 9.81, it simplifies the math right down to a single number (125.85) before storing or printing it.
  4. Notebook Cells Remember State: Code cells in the same Jupyter notebook share the same memory! If you define g = 9.80665 in Cell 1 and press Shift + Enter, Cell 2 already knows g.

Tip (Markdown + Code Cells): A great engineering notebook combines Markdown cells (explanations, assumptions, equations like \(P = \tau\omega\)) with Code cells (live executable math). Your future self—and any design reviewer—can trace every number back to its assumption.

# Example 1 (ME 200 Thermodynamics Intro): Compressed Air Tank Inventory
# Suppose a 0.25 m^3 workshop air receiver sits at P = 850 kPa (abs) and T = 27 degC.
# Never use Celsius in PV = mRT! Always convert to Kelvin first.

pressure_kpa = 850.0          # Absolute pressure [kPa]
volume_m3 = 0.25              # Tank internal volume [m^3]
temp_c = 27.0                 # Gas temperature [degC]
temp_k = temp_c + 273.15      # Absolute temperature [K]
R_air = 0.287                 # Specific gas constant for air [kJ/(kg*K)]

mass_kg = (pressure_kpa * volume_m3) / (R_air * temp_k)
density_kg_m3 = mass_kg / volume_m3

print("=== Air Receiver Inventory Check (ME 200 Ideal Gas Law) ===")
print(f"Temperature   : {temp_c:.1f} degC = {temp_k:.2f} K")
print(f"Air Mass      : {mass_kg:.4f} kg")
print(f"Air Density   : {density_kg_m3:.3f} kg/m^3")
=== Air Receiver Inventory Check (ME 200 Ideal Gas Law) ===
Temperature   : 27.0 degC = 300.15 K
Air Mass      : 2.4668 kg
Air Density   : 9.867 kg/m^3

Notice three habits in those 12 lines that will save you hours later:

  1. Put units in variable names (pressure_kpa, volume_m3, temp_k). Mixing up Pa and kPa or degC and K is the #1 bug in engineering code—not syntax errors!
  2. Use # comments to write short reminders of why a constant has a particular value (like R_air = 0.287 # kJ/(kg*K)).
  3. Use print(f"... {var:.4f} units"), called an f-string, so Python inserts variables cleanly inside readable text instead of dumping unlabelled numbers like 2.467113468905.

Watch Out! Three First-Day Gotchas

  1. Math Symbols Differ from Textbooks: Multiplication MUST use * (m * g * h, never mgh or m(g)). Exponentiation uses double-star ** (D**2, NOT D^2—in Python ^ means bitwise XOR, which gives completely wrong answers on integers and crashes on floats!).
  2. Case Sensitivity: Temp_K and temp_k are two completely separate variables in Python. Pick lowercase snake_case consistently.
  3. Cell Execution Order Matters: If you edit Cell 1 after running Cell 2, Cell 2 still holds the old result until you re-run Cell 1 and Cell 2 in order (Run All).

Let’s run two more mini engineering workflows right away—one from Machine Design / Vibrations and one from Fluid Mechanics & Solar Thermal—so you see how naturally Python bridges your courses.

# Example 2 (Machine Design & Dynamics): Gearbox Shaft Torque and Mechanical Power
import math  # Standard library module with pi, sin, cos, sqrt, etc.

rpm = 1750.0                  # Electric motor speed [rev/min]
power_kw = 18.5               # Rated shaft power [kW]
gear_ratio = 4.2              # Speed reduction ratio (omega_in / omega_out)
gear_efficiency = 0.96        # Helical gear mesh efficiency [-]

omega_in = 2.0 * math.pi * rpm / 60.0       # Input angular speed [rad/s]
torque_in = (power_kw * 1000.0) / omega_in  # Torque T = P / omega [N*m]

omega_out = omega_in / gear_ratio
power_out_kw = power_kw * gear_efficiency
torque_out = (power_out_kw * 1000.0) / omega_out

print("=== Gearbox Sizing Summary ===")
print(f"Input Speed   : {omega_in:7.2f} rad/s | Input Torque  : {torque_in:7.2f} N*m")
print(f"Output Speed  : {omega_out:7.2f} rad/s | Output Torque : {torque_out:7.2f} N*m")
print(f"Heat Rejected : {(power_kw - power_out_kw)*1000:.1f} W")
=== Gearbox Sizing Summary ===
Input Speed   :  183.26 rad/s | Input Torque  :  100.95 N*m
Output Speed  :   43.63 rad/s | Output Torque :  407.03 N*m
Heat Rejected : 740.0 W
# Example 3 (Fluid Mechanics + Solar Thermal): Quick Peak Cooling-Loop Flow Estimate
# A rooftop solar thermal array rejects Q_dot = 12.0 kW to water (cp = 4.184 kJ/(kg*K))
# with a collector temperature rise delta_T = 8.0 K inside a D = 25 mm copper tube.

q_dot_kw = 12.0               # Thermal heat rate [kW]
cp_water = 4.184              # Specific heat [kJ/(kg*K)]
delta_T_k = 8.0               # Water temperature rise [K]
rho_water = 997.0             # Water density [kg/m^3]
diameter_m = 0.025            # Inner tube diameter [m]

m_dot_kg_s = q_dot_kw / (cp_water * delta_T_k)
vol_flow_m3_s = m_dot_kg_s / rho_water
area_m2 = math.pi * (diameter_m ** 2) / 4.0
velocity_m_s = vol_flow_m3_s / area_m2

print("=== Solar Collector Piping Quick-Check ===")
print(f"Mass Flow Rate : {m_dot_kg_s:.4f} kg/s ({vol_flow_m3_s * 1000:.3f} L/s)")
print(f"Bulk Velocity  : {velocity_m_s:.3f} m/s (target < 2.0 m/s avoids erosion)")
=== Solar Collector Piping Quick-Check ===
Mass Flow Rate : 0.3585 kg/s (0.360 L/s)
Bulk Velocity  : 0.733 m/s (target < 2.0 m/s avoids erosion)

Practice Questions (Concept Check)

  1. Why does writing 5**2 give 25, whereas 5^2 gives 7 in Python?
  2. What happens if you forget to import math before writing math.pi?
  3. Why is temperature_c = 25.0 followed by temperature_k = temperature_c + 273.15 safer than overwriting t = t + 273.15?
  4. Suppose you run Cell 2 before running Cell 1 after restarting your Jupyter kernel. What error does Python raise?

Guided Practice Problem 1 (Worked Self-Check): Cantilever Beam Tip Check

A \(2.0\text{-m}\) aluminum cantilever beam (\(E = 69\text{ GPa}\), width \(b = 40\text{ mm}\), height \(h = 60\text{ mm}\)) carries an end tip load \(F = 850\text{ N}\) (cross-referencing Pre-Calculus & Calculus I Week 5: Cantilever Beam Deflection). Write a script that calculates:

Hint: Watch Unit Consistency (Convert mm to m Right Away!)

Convert b = 0.040, h = 0.060, and E = 69e9 so every formula operates cleanly in base SI units (N, m, Pa). Multiply \(\delta_{\max}\) by 1000 at the end for mm, and divide \(\sigma_{\max}\) by 1e6 for MPa.

# Worked Practice Problem 1 Solution: Cantilever Beam Static Check
F_N = 850.0          # Tip point load [N]
L_m = 2.0            # Beam span [m]
E_Pa = 69e9          # Al 6061 Young's modulus [Pa]
b_m = 0.040          # Width [m]
h_m = 0.060          # Height [m]

I_m4 = (b_m * (h_m ** 3)) / 12.0
delta_m = (F_N * (L_m ** 3)) / (3.0 * E_Pa * I_m4)
sigma_pa = (F_N * L_m * (h_m / 2.0)) / I_m4

print(f"Moment of Inertia I : {I_m4:.4e} m^4")
print(f"Tip Deflection      : {delta_m * 1000.0:.3f} mm")
print(f"Root Bending Stress : {sigma_pa / 1e6:.2f} MPa (Al 6061 Sy = 275 MPa)")
Moment of Inertia I : 7.2000e-07 m^4
Tip Deflection      : 45.625 mm
Root Bending Stress : 70.83 MPa (Al 6061 Sy = 275 MPa)

End-of-Chapter Practice Problems (Try Yourself!)

  1. Carnot Upper Limit (ME 200): A geothermal steam turbine operates between a brine source at \(T_H = 195^\circ\text{C}\) and a cooling tower at \(T_C = 28^\circ\text{C}\). Compute the Carnot maximum efficiency \(\eta_{\text{Carnot}} = 1 - T_C/T_H\) (in Kelvin!) and the minimum heat input rate \(\dot{Q}_H\) needed for \(\dot{W}_{\text{net}} = 4.5\text{ MW}\).
  2. Venturi Throat Velocity (Fluid Mechanics): Air (\(\rho = 1.20\text{ kg/m}^3\)) enters a Venturi duct at \(D_1 = 120\text{ mm}\), \(V_1 = 8.5\text{ m/s}\), and narrows to \(D_2 = 60\text{ mm}\). Using continuity \(A_1 V_1 = A_2 V_2\) and Bernoulli \(\Delta P = \frac{1}{2}\rho(V_2^2 - V_1^2)\), find \(V_2\) and \(\Delta P\) in \(\text{Pa}\).
  3. Composite Furnace Wall Heat Flux (Heat & Mass Transfer): A furnace wall consists of \(15\text{ cm}\) firebrick (\(k_1 = 0.90\text{ W/(m}\cdot\text{K)}\)) and \(10\text{ cm}\) mineral wool (\(k_2 = 0.045\text{ W/(m}\cdot\text{K)}\)) with inner temperature \(850^\circ\text{C}\) and outer temperature \(45^\circ\text{C}\). Compute thermal resistance per unit area \(R''_{\text{tot}} = L_1/k_1 + L_2/k_2\) and steady heat flux \(q'' = \Delta T / R''_{\text{tot}}\).

🎬 Concept & Engineering Animation

🎬 Calculator Vs Python Workflow