flowchart TD
A["1. Your Browser<br/>(Google Colab Code Cell)"] -->|"Press Shift + Enter"| B["2. Python Interpreter<br/>(Checks Grammar / Syntax)"]
B -->|"Evaluates Line by Line"| C["3. CPU & Working Memory<br/>(Stores Values & Computes Math)"]
C -->|"Sends Printed Output Back"| D["4. Output Area Below Cell<br/>(Text, Numbers, Tables, or Error)"]
1 How Computers Think, Google Colab, and Your Very First Program
Chapter Mission: Demystify what a computer program actually is, launch your first cloud Python environment in Google Colab without installing anything, write and dissect your first epidemiological calculations line-by-line, and lose all fear of Python error messages forever.
Chapter Companion Notebooks (1-to-1 Mirrored in Google Colab)
| Lab Type | Interactive Colab Notebook | Scan (Print) |
|---|---|---|
| Walkthrough | ch01 / 01_walkthrough.ipynb | ![]() |
| Guided To-Dos | ch01 / 02_guided_todos.ipynb | ![]() |
| Coding Exam | ch01 / 03_coding_exam.ipynb | ![]() |
Learning Objectives
By the end of this chapter, you will be able to:
- Explain how the Python interpreter reads and executes code line-by-line inside a Google Colab cloud runtime.
- Write clean
print()statements, human-readable#comments, and basic arithmetic expressions (+,-,*,/,//,%,**). - Apply operator precedence and explicit parentheses so statistical averages and epidemiological rates are never silently corrupted.
- Calmly read and fix four foundational Python errors:
SyntaxError,NameError,TypeError, andZeroDivisionError. - Structure any beginner data problem using the 4-Step Problem-Structuring Blueprint before typing Python code.
1.1 Stage 1 — First-Principles Intuition: How Computers Actually Think
Before touching a keyboard, let us clear up the biggest myth in programming:
Computers are not intelligent. A computer is an astonishingly fast, 100% literal clerk who follows a checklist one line at a time—and never guesses what you “meant.”
Imagine a newly hired clerk at a rural Upazila Health Complex who has zero medical background, cannot improvise, and obeys written instructions literally:
- If you write: “Measure temperature and write down if it is high,” the clerk freezes—what does “high” mean? Celsius or Fahrenheit? Where should it be written?
- If instead you write an unambiguous numbered checklist:
- Take the thermometer reading in Celsius (
38.5). - Multiply that number by
9, divide by5, and add32. - Print the resulting Fahrenheit number on the intake slip.
- Take the thermometer reading in Celsius (
The clerk executes those three steps accurately ten million times per second without ever getting tired or making an arithmetic slip.
A Python program is simply that exact numbered checklist—written in a tiny, unambiguous language called Python.
1.1.1 Why Python for Statistics, Public Health, and Machine Learning?
You may wonder why researchers worldwide—from WHO epidemiologists to deep learning labs—converged on Python:
- Readable Like English:
mean_sbp = (124 + 130 + 127) / 3reads almost identically to a biostatistics notebook. - One Language from First Line to First Paper: Unlike tools that trap you inside point-and-click menus, Python handles raw messy household surveys (Pandas), classical hypothesis tests (SciPy/Statsmodels), gradient-boosted trees (XGBoost), and neural networks on GPUs (PyTorch) in one unified workflow.
- Reproducibility: When a peer reviewer asks, “How did you compute your maternal anemia prevalence after excluding missing hemoglobin values?”, a Python script answers with exact mathematical transparency.
1.2 Stage 2 — Visual Mental Model: Inside Google Colab & the Python Interpreter
When you open one of this book’s .ipynb notebooks in Google Colab, you are not running code on your laptop’s processor—you are sending Python instructions over the internet to a clean Linux computer hosted by Google (the Colab Runtime).
Notice the two distinct checkpoints in that pipeline:
- Grammar Check (Syntax Parsing): Before running any line in a cell, Python scans your grammar. If you forgot a closing quotation mark or parenthesis, Python stops immediately with a
SyntaxError. - Step-by-Step Execution (Runtime): Once grammar passes, Python executes Line 1, then Line 2, then Line 3 in strict top-to-bottom order. If Line 3 asks Python to divide by zero, Lines 1 and 2 succeed and Line 3 stops with a
ZeroDivisionError.
When you click any Open in Colab link from this book, Google opens our master read-only copy. Always click File \(\rightarrow\) Save a copy in Drive immediately so every note, edit, and solved exercise stays safely inside your personal Google Drive folder.
1.3 Stage 3 — Line-by-Line Code Walkthrough & Error Anatomy
Open part_01_python_foundations/ch01_first_program/01_walkthrough.ipynb in Colab and run each cell alongside the line-by-line dissections below.
1.3.1 Your Very First print() Statements & Comments
Expected Output:
Line-by-Line Dissection:
| Line | Code Element | What Python Does Internally |
|---|---|---|
1 |
# Lines starting with '#' ... |
Sees # and skips everything to the end of the line. Comments explain why to future humans (especially your future self six months later!). |
2 |
print(...) |
Calls Python’s built-in function print, which displays whatever sits inside its parentheses (...) onto the screen. |
2 |
"Upazila Health Complex ..." |
Quotation marks tell Python: “Treat these characters literally as text (a string, or str), not as Python commands.” |
3 |
"Patients waiting in queue:", 14 |
A comma inside print() lets you pass multiple items (here, a text string and the whole number 14). Python automatically inserts one clean space between them. |
1.3.2 Python as a Scientific & Epidemiological Calculator
Every quantitative researcher needs exact command over Python’s seven arithmetic operators:
# Basic arithmetic operators in Python
print("Addition (+):", 120 + 45)
print("Subtraction (-):", 120 - 45)
print("Multiplication (*):", 14 * 3)
print("True Division (/):", 10 / 4) # Always returns float: 2.5
print("Floor Division (//):", 10 // 4) # Whole groups only: 2
print("Remainder / Modulo (%):", 10 % 4) # Leftover after packing: 2
print("Exponentiation (**):", 1.65 ** 2) # Height squared for BMI: 2.7225Expected Output:
Let us zoom in on the three operators that surprise beginners most:
- True Division (
/) vs. Floor Division (//):10 / 4gives2.5(a decimal number, called afloat). Even8 / 2gives4.0!10 // 4asks: “How many complete groups of 4 fit inside 10?” Answer:2(an exact whole number, called anint). Think of packing vaccine vials into 24-vial cold boxes.
- Modulo / Remainder (
%):10 % 4asks: “After removing those complete groups of 4, how many items are left over?” Since \(2 \times 4 = 8\), the leftover is \(10 - 8 = 2\).- Golden Packing Invariant: For any positive integers \(a\) and \(b\),
(a // b) * b + (a % b) == a.
- Exponentiation (
**), NOT^:- To square height (\(1.65^2\)) for Body Mass Index, write
1.65 ** 2. - Never write
1.65 ^ 2—in Python,^is a completely different operator (bitwise XOR) that will crash on decimals and give wrong answers on integers!
- To square height (\(1.65^2\)) for Body Mass Index, write
1.3.3 Operator Precedence: Why Parentheses Save Lives in Biostatistics
Python follows standard mathematical PEMDAS order: Parentheses \(\rightarrow\) Exponents (**) \(\rightarrow\) Multiplication / Division (*, /, //, %) \(\rightarrow\) Addition / Subtraction (+, -).
Run this experiment in Cell 3 of 01_walkthrough.ipynb:
Expected Output:
Notice that wrong_mean = 120 + 130 + 140 / 3 did not produce a red error! Python happily calculated 140 / 3 = 46.67 first, then added 120 + 130 + 46.67 = 296.67—claiming the average systolic blood pressure of three normotensive adults is a fatal 296.7 mmHg!
Researcher’s Rule: Whenever you divide a sum, multiply a rate, or convert units, use explicit parentheses even when you think you know the precedence rule. Clarity beats cleverness every single time.
1.3.4 Break It On Purpose! (Error Anatomy Lab)
Beginners often treat red Python error boxes as a personal failure. Senior researchers treat them as free diagnostic reports. Let us trigger three classic errors deliberately so you can dissect how to read them from the bottom line upward.
Python’s Traceback:
Diagnosis: The + symbol glues two strings together ("A" + "B") OR adds two numbers (1 + 2), but refuses to glue a string and a float directly.
Fix: Pass separate arguments with a comma (print("Fever temperature:", 38.5)) or use an f-string (print(f"Fever temperature: {38.5}")).
Python’s Traceback:
Diagnosis: In epidemiology, missing or unpopulated catchment records sometimes enter datasets as 0. Dividing by zero is mathematically undefined, so Python halts cleanly rather than silently inventing a bogus incidence rate. (In Chapter 4 and Chapter 10, we will guard against this defensively!)
1.4 Stage 4 — The Researcher’s Problem-Structuring Blueprint
Let us apply our signature 4-Step Problem-Structuring Blueprint to a realistic clinical intake task before writing code.
Scenario: A nurse at an Upazila clinic enters a patient’s ID ("UHC-0001"), weight (58.4 kg), height (160.0 cm), and body temperature (38.6 °C). Print a structured intake card showing Body Mass Index (\(\text{kg/m}^2\), rounded to 2 decimals) and temperature in both Celsius and Fahrenheit (rounded to 1 decimal).
1.4.1 Blueprint (Completed on Paper Before Coding)
- Inputs & Units:
patient_id = "UHC-0001"(str)weight_kg = 58.4(float, kilograms)height_cm = 160.0(float, centimeters — notice the unit!)temp_c = 38.6(float, degrees Celsius)
- Edge Cases & Traps:
- Unit Trap: BMI requires height in meters, not centimeters! Forgetting to divide
height_cm / 100produces an absurd BMI of0.0023. - Exponent Trap: Square meters using
height_m ** 2, notheight_m * 2. - Precedence Trap: Convert Celsius to Fahrenheit as
(temp_c * 9 / 5) + 32.
- Unit Trap: BMI requires height in meters, not centimeters! Forgetting to divide
- Step-by-Step Algorithm:
- Convert
height_cmtoheight_mby dividing by100. - Compute
bmi = round(weight_kg / (height_m ** 2), 2). - Compute
temp_f = round((temp_c * 9 / 5) + 32, 1). - Print a clean, aligned intake card using f-strings.
- Convert
- Verification Plan (Hand Check):
- \(160\text{ cm} = 1.60\text{ m}\); \(1.60^2 = 2.56\); \(58.4 / 2.56 = 22.8125 \approx 22.81\) (healthy adult range: 18.5–24.9).
- \(38.6 \times 1.8 + 32 = 69.48 + 32 = 101.48 \approx 101.5^\circ\text{F}\) (consistent with mild-to-moderate fever).
1.4.2 Clean Python Implementation (Verbatim in 01_walkthrough.ipynb)
# Step 1: Inputs (documented with explicit units!)
patient_id = "UHC-0001"
weight_kg = 58.4
height_cm = 160.0
temp_c = 38.6
# Step 2: Algorithm (convert cm -> m, compute BMI, convert C -> F)
height_m = height_cm / 100
bmi = round(weight_kg / (height_m ** 2), 2)
temp_f = round((temp_c * 9 / 5) + 32, 1)
# Step 3: Clean Structured Output
print("=== COMMUNITY CLINIC INTAKE CARD ===")
print(f"Patient ID : {patient_id}")
print(f"BMI : {bmi} kg/m^2")
print(f"Temperature: {temp_c} C ({temp_f} F)")Output:
Notice how writing the unit directly inside the variable name (weight_kg, height_cm, height_m, temp_c, temp_f) makes unit-mismatch bugs immediately visible to the human eye. This is a lifelong research software engineering habit.
1.5 Stage 5 — Guided “To-Do” Practice Labs
Open part_01_python_foundations/ch01_first_program/02_guided_todos.ipynb in Google Colab, run Step 0 to activate sharukh-tutor, and solve all six exercises below. Use tutor.hint("ch01_todo_XX", level=1..3) if you need a progressive nudge, and run tutor.check(...) after each solution.
To-Do 1.1 (Warm-Up — Clinic Intake Banner): Create a string variable
clinic_bannerset to"Upazila Health Complex - Patient Intake Active".To-Do 1.2 (Core Arithmetic — Fever Conversion): Given
temp_c = 38.5, compute Fahrenheit using \(F = (C \times \frac{9}{5}) + 32\) and store the result intemp_f.To-Do 1.3 (Public Health Metric — Patient BMI): Given
weight_kg = 62.0andheight_m = 1.65, compute \(\text{BMI} = \frac{\text{weight}}{\text{height}^2}\) rounded to 2 decimal places (round(..., 2)), stored inpatient_bmi.To-Do 1.4 (Cold-Chain Logistics — Vaccine Box Packing): An outreach clinic receives
275vaccine vials; each cold box holds24vials. Computefull_boxes(using//) andleftover_vials(using%).To-Do 1.5 (Epidemiology — Weekly Dengue Incidence Rate): A district of
240000residents recorded186confirmed dengue cases. Computeincidence_per_100k = round((cases / population) * 100000, 2).To-Do 1.6 (Stretch Debugging — Fix the Broken Blood Pressure Average): Correct the type bug and operator-precedence bug in:
so that
mean_sbpcomputes the true average (127.0).
1.6 Stage 6 — Concept & Execution Trace Test (No Computer Allowed!)
Before opening the Coding Exam, test your mental model on paper. Check your answers against Stage 8.
Predict the Output: What exact number does
15 // 4evaluate to? What about15 % 4?Predict the Output: What data type (
intorfloat) does12 / 3return in Python?Spot the Error Type: Running
print(Chattogram)without quotes triggers which exact Python error?Spot the Error Type: Running
print("Age: " + 29)triggers which exact Python error?Operator Precedence Trace: Evaluate
10 + 20 * 3by hand, then evaluate(10 + 20) * 3.Exponent Trap: What does
3 ** 2return? Why should you never write3 ^ 2for squaring in Python?Execution Trace Table: Fill in the values after each line executes:
Silent Bug Audit: A student writes
bmi = 70 / 1.75 * 1.75. What number does Python compute, and why is it dangerously wrong?Unit Hygiene: Why is
height_cm = 165superior toh = 165in public health code?Colab Discipline: Why must you click File \(\rightarrow\) Save a copy in Drive before editing a shared course notebook?
1.7 Stage 7 — Chapter 1 Hands-On Coding Exam (100 Points)
Open part_01_python_foundations/ch01_first_program/03_coding_exam.ipynb, complete all three tasks using the 4-Step Problem-Structuring Blueprint, and run tutor.check_exam("ch01", exam_submissions) for your automated 100-point scorecard.
1.7.1 Exam Task 1.1 (30 Points): Pediatric ORS Supply & Carton Logistics
A community clinic projects 140 pediatric diarrhea patients this week, each requiring 3 Oral Rehydration Solution (ORS) packets, plus a 15% emergency safety buffer (* 1.15). Each shipping carton holds 50 packets. Compute:
base_packets(int)total_packets(int, computed withround(base_packets * buffer_multiplier)to avoid binary float truncation)full_cartons(int) andextra_packets(int)
1.7.2 Exam Task 1.2 (35 Points): Maternal Health Clinic Summary Card
Out of 320 registered mothers in Q1, 218 completed 4+ antenatal visits and 96 tested positive for maternal anemia. Compute:
anc_coverage_pct(rounded to 1 decimal)anemia_prevalence_pct(rounded to 1 decimal)summary_line: exact formatted report"ANC 4+ Coverage: 68.1% | Anemia Prevalence: 30.0%"generated dynamically using an f-string.
1.7.3 Exam Task 1.3 (35 Points): Rural vs. Urban Case Fatality Rate Disparity
Surveillance reports record 450 rural cases with 9 deaths, and 600 urban cases with 6 deaths. Compute:
cfr_rural_pct: \(\left(\frac{9}{450}\right) \times 100\)cfr_urban_pct: \(\left(\frac{6}{600}\right) \times 100\)cfr_ratio:cfr_rural_pct / cfr_urban_pct
1.8 Stage 8 — Complete Annotated Solutions & Debugging Post-Mortem
1.8.1 A. Concept & Execution Trace Test Key
15 // 4is3;15 % 4is3(check: \(3 \times 4 + 3 = 15\)).float(4.0) — true division/always returns afloat.NameError: name 'Chattogram' is not defined.TypeError: can only concatenate str (not "int") to str.10 + 20 * 3 = 70;(10 + 20) * 3 = 90.3 ** 2 = 9. In Python,^is bitwise XOR (3 ^ 2 == 1!), not exponentiation.- After Line 1:
cases = 40. After Line 2:cases = 55. After Line 3:cases = 55,double_cases = 110. - Python evaluates
(70 / 1.75) * 1.75 = 70.0(division and multiplication execute left-to-right)! It must be70 / (1.75 ** 2)or70 / (1.75 * 1.75). - Encoding the unit (
_cm) prevents silent cm-vs-meter errors during data cleaning and modeling. - Shared links are read-only; saving a copy to your Google Drive preserves your work permanently.
1.8.2 B. Annotated Solutions — Guided To-Dos (Mirrored in 02_guided_todos_SOLUTION.ipynb)
# To-Do 1.1
clinic_banner = "Upazila Health Complex - Patient Intake Active"
# To-Do 1.2
temp_c = 38.5
temp_f = (temp_c * 9 / 5) + 32 # 101.3
# To-Do 1.3
weight_kg = 62.0
height_m = 1.65
patient_bmi = round(weight_kg / (height_m ** 2), 2) # 22.77
# To-Do 1.4
total_vials = 275
box_capacity = 24
full_boxes = total_vials // box_capacity # 11
leftover_vials = total_vials % box_capacity # 11
# To-Do 1.5
cases = 186
population = 240000
incidence_per_100k = round((cases / population) * 100000, 2) # 77.5
# To-Do 1.6
sbp_1 = 124
sbp_2 = 130
sbp_3 = 127
mean_sbp = (sbp_1 + sbp_2 + sbp_3) / 3 # 127.01.8.3 C. Annotated Solutions — Chapter 1 Coding Exam (Mirrored in 03_coding_exam_SOLUTION.ipynb)
# Exam Task 1.1 (30 pts)
patients = 140
packets_per_patient = 3
buffer_multiplier = 1.15
carton_size = 50
base_packets = patients * packets_per_patient # 420
total_packets = round(base_packets * buffer_multiplier) # 483
full_cartons = total_packets // carton_size # 9
extra_packets = total_packets % carton_size # 33
# Exam Task 1.2 (35 pts)
total_mothers = 320
anc_4plus = 218
anemic_count = 96
anc_coverage_pct = round((anc_4plus / total_mothers) * 100, 1) # 68.1
anemia_prevalence_pct = round((anemic_count / total_mothers) * 100, 1) # 30%
summary_line = (
f"ANC 4+ Coverage: {anc_coverage_pct}% | "
f"Anemia Prevalence: {anemia_prevalence_pct}%"
)
# Exam Task 1.3 (35 pts)
rural_cases, rural_deaths = 450, 9
urban_cases, urban_deaths = 600, 6
cfr_rural_pct = (rural_deaths / rural_cases) * 100 # 2.0
cfr_urban_pct = (urban_deaths / urban_cases) * 100 # 1.0
cfr_ratio = cfr_rural_pct / cfr_urban_pct # 2.01.8.4 D. Debugging Post-Mortem: Top 3 Chapter 1 Failure Modes
- The Famous IEEE-754 Binary Float Trap (
int(420 * 1.15) == 482!): Because computers store decimals in base-2 binary,420 * 1.15evaluates internally to482.99999999999994. Callingint(...)chops off everything after the decimal point and drops a packet (482), whereasround(...)properly rounds to483. We will unpack binary floating-point representation deeply in Chapter 2. - Hardcoding Numbers Inside Summary Strings: Typing
"ANC 4+ Coverage: 68.1%"manually instead of using{anc_coverage_pct}defeats the entire purpose of reproducible research—the next time Q2 clinic counts arrive, your printed report will silently contradict your data. - Inverting Epidemiological Ratios: Writing
cfr_urban_pct / cfr_rural_pctyields0.5instead of2.0. Always label which group is your reference (denominator) group—an essential habit we carry directly into logistic regression odds ratios in Chapter 20 and Chapter 23.


