flowchart LR
subgraph Names["Name Tags (Namespace)"]
N1["cases"]
end
subgraph Memory["Python Object Memory"]
O1["Object A (old)<br/>type: int | val: 1000"]
O2["Object B (new)<br/>type: int | val: 1250"]
end
N1 -. "1. Initial binding" .-> O1
N1 ==>|"2. Rebound after + 250"| O2
2 Variables, Memory Models, Data Types, and Arithmetic Logic
Chapter Mission: Build a rock-solid mental model of how Python stores data in memory using name bindings (not boxes), master Python’s core primitive types (
int,float,str,bool, andNone), conquer real-world survey type-casting traps, and solve the binary floating-point mystery from Chapter 1 once and for all.
Chapter Companion Notebooks (1-to-1 Mirrored in Google Colab)
| Lab Type | Interactive Colab Notebook | Scan (Print) |
|---|---|---|
| Walkthrough | ch02 / 01_walkthrough.ipynb | ![]() |
| Guided To-Dos | ch02 / 02_guided_todos.ipynb | ![]() |
| Coding Exam | ch02 / 03_coding_exam.ipynb | ![]() |
Learning Objectives
By the end of this chapter, you will be able to:
- Explain why Python variables are name tags pointing to typed objects in memory, and trace variable rebinding using
id()and execution trace tables. - Choose and inspect Python’s five foundational data types (
int,float,str,bool, andNoneType) usingtype()andisinstance(). - Update running counters cleanly with augmented assignment operators (
+=,-=,*=,/=). - Safely convert messy text strings exported from CSV/DHS surveys into exact numbers—including the two-step
int(float("24.0"))idiom. - Explain why
0.1 + 0.2 != 0.3in binary floating-point math and compare decimals reliably usingmath.isclose(). - Compute core biostatistics metrics—including clinical screening flags and \(2 \times 2\) contingency table Odds Ratios—without silent type or rounding errors.
2.1 Stage 1 — First-Principles Intuition: Sticky-Note Name Tags, Not Storage Boxes
Many introductory books tell beginners: “A variable is a box that holds a value.” That analogy feels cozy on Day 1—and then breaks painfully in Chapter 7 (when two list names point to the same patient list!) and Chapter 28 (when PyTorch tensors share GPU memory).
Let us learn the true Python mental model from Day 1.
Picture a busy clinical laboratory shelf holding sealed sample tubes (Objects in Memory). Every tube has three permanent traits stamped on it the moment it is created:
- A Type (e.g.,
int,float,str,bool) — what kind of specimen it is. - A Value (e.g.,
1000,10.6,"UHC-0042",True) — the data inside. - A Memory Address (
id) — its exact physical slot on the shelf.
When you write cases = 1000, Python does not stuff 1000 into a box named cases. Instead, Python creates an int object holding 1000 on the memory shelf and attaches a sticky-note name tag labeled cases pointing to that object.
When you later write cases = cases + 250, Python computes a brand-new int object (1250) in a new memory slot, peels the cases sticky note off 1000, and sticks it onto 1250!
2.1.1 Variable Naming Rules & Researcher Hygiene
Python enforces four strict grammar rules for variable names:
- May contain only letters (
a-z,A-Z), digits (0-9), and underscores (_). - Cannot start with a digit (
visit_2is valid;2nd_visittriggers aSyntaxError). - No spaces (
patient_ageis valid;patient ageis invalid). - Cannot reuse Python reserved keywords (such as
if,for,class,return,True,None).
Beyond what Python requires, disciplined researchers follow PEP 8 snake_case + explicit units:
| Poor Name | Disciplined Researcher Name | Why It Matters |
|---|---|---|
w = 62.0 |
weight_kg = 62.0 |
Prevents kilograms-vs-pounds confusion. |
h = 165 |
height_cm = 165 |
Immediately alerts you to divide by 100 for BMI. |
f = True |
has_fever = True |
has_ / is_ prefixes scream “this is a bool!” |
x1 = 10.4 |
maternal_hb_g_dl = 10.4 |
Self-documenting six months later during peer review. |
2.2 Stage 2 — Visual Mental Model: Object Creation & Rebinding in Memory
Watch what happens inside memory across two lines of Python code:
Notice the crucial direction of =: in mathematics, \(x = x + 1\) is impossible. In Python, = is NOT mathematical equality—it is the assignment (binding) arrow:
Golden Rule of Assignment: Python always evaluates the entire right-hand side first to produce an object, and then attaches the left-hand side name tag to that resulting object.
2.3 Stage 3 — Line-by-Line Code Walkthrough & Error Anatomy
Open part_01_python_foundations/ch02_variables_types/01_walkthrough.ipynb in Colab and run every cell alongside the dissections below.
2.3.1 Proving Rebinding with id()
When you run this in Colab, you will see two completely different Memory IDs—direct experimental proof that cases moved to a brand-new int object (1250) rather than mutating 1000 in place!
2.3.2 The Four Core Primitive Types + None
Every piece of clinical, demographic, or statistical data you will work with starts as one of five foundational Python types:
patient_id = "UHC-0042" # str (text)
age_years = 29 # int (exact whole number)
hb_g_dl = 10.6 # float (decimal measurement)
is_anemic = hb_g_dl < 11.0 # bool (True or False)
follow_up_date = None # NoneType (explicitly missing / not recorded)
print(patient_id, "->", type(patient_id))
print(age_years, "->", type(age_years))
print(hb_g_dl, "->", type(hb_g_dl))
print(is_anemic, "->", type(is_anemic))
print(follow_up_date, "->", type(follow_up_date))Expected Output:
Never store a missing lab measurement as 0! A patient with hb_g_dl = 0 is clinically dead—and including 0 in an average hemoglobin calculation will severely bias your entire study downward. In pure Python, None explicitly represents “no value recorded.” (In Part IV, Pandas extends this idea with NaN.)
2.3.3 Augmented Assignment & Execution Trace Tables
Updating a running total—such as vaccine vials in stock or cumulative outbreak cases—happens constantly in code. Python provides shorthand augmented assignment operators:
| Shorthand | Exact Meaning | Clinic Example |
|---|---|---|
x += 5 |
x = x + 5 |
waiting_patients += 5 |
x -= 3 |
x = x - 3 |
vaccine_stock -= 85 |
x *= 2 |
x = x * 2 |
buffer_dose *= 1.15 |
x /= 4 |
x = x / 4 |
total_sbp /= 3 |
Execution Trace Table (Step-by-Step State):
| Step | Executed Statement | vaccine_stock Before |
vaccine_stock After |
|---|---|---|---|
1 |
vaccine_stock = 500 |
(unbound) | 500 |
2 |
vaccine_stock -= 85 |
500 |
415 |
3 |
vaccine_stock += 200 |
415 |
615 |
2.3.4 Explicit Type Conversion (Casting Messy Survey Strings)
When you read raw text files, web forms, or CSV exports from DHS/MICS surveys, every field arrives as a string (str)—even ages and blood pressures ("34", "38.5", "24.0"). Before doing math, you must explicitly cast them:
# Raw CSV rows always arrive as strings!
raw_age = "34"
raw_temp = "38.5"
raw_months = "24.0"
age_int = int(raw_age)
temp_float = float(raw_temp)
months_int = int(float(raw_months)) # Two-step idiom for decimal-strings!
print(age_int, type(age_int))
print(temp_float, type(temp_float))
print(months_int, type(months_int))Expected Output:
Two casting traps catch almost every beginner:
int("24.0")raisesValueError: Python’sint()expects pure whole-number digits when given a string. Whenever SPSS, Stata, or Pandas exports integer counts with a.0suffix ("24.0"), always chainint(float(raw_months)).int(3.9)evaluates to3, NOT4:int()truncates toward zero—it violently chops off everything after the decimal point. Always useround(3.9)when you want nearest-integer rounding!
2.3.5 Solving Chapter 1’s Mystery: Binary Floating-Point Precision
Remember the surprise in Chapter 1’s Coding Exam where 420 * 1.15 evaluated to 482.99999999999994? Here is the exact mathematical reason—and it takes just 60 seconds to understand permanently.
In base-10 (human decimal math), the fraction \(\frac{1}{3}\) cannot be written finitely: \(0.3333333\ldots\) repeats forever. Computers do not count in base-10; their transistors only have two states (0 and 1, base-2 binary). In base-2, simple base-10 decimals like \(0.1 = \frac{1}{10}\) and \(1.15 = \frac{23}{20}\) are repeating infinite binary fractions! Under the universal IEEE-754 64-bit floating-point standard, Python stores the closest 53-bit binary approximation.
Run Cell 5 of 01_walkthrough.ipynb to see this live:
import math
print("0.1 + 0.2 =", 0.1 + 0.2)
print("0.1 + 0.2 == 0.3 ->", (0.1 + 0.2) == 0.3)
print("math.isclose() ->", math.isclose(0.1 + 0.2, 0.3))
# Paying off Chapter 1's ORS supply mystery:
print("420 * 1.15 =", 420 * 1.15)
print("int(420 * 1.15) =", int(420 * 1.15), " <-- chops decimal!")
print("round(420 * 1.15)=", round(420 * 1.15), " <-- nearest integer!")Expected Output:
Across statistics, econometrics, and deep learning tests, never write a == b on floats. Always use math.isclose(a, b) (or np.isclose(a, b) in Part IV), which checks whether two decimals agree within tiny numerical tolerance (default relative tolerance \(10^{-9}\)).
2.3.6 Break It On Purpose! (Type & Casting Error Anatomy)
Python’s Traceback:
Diagnosis: int() received the right type (str), so it is not a TypeError—but the value inside the string contains a dot ., which is not a base-10 integer digit.
Fix: Convert through float() first: months = int(float("24.0")).
Python’s Traceback:
Diagnosis: Real public health CSVs routinely encode missing lab readings as "N/A", ".", or "unknown". Calling float() directly crashes cleanly rather than inventing a fake number. (In Chapters 4 and 10, we will detect "N/A" and convert it safely to None!)
Python’s Traceback:
Diagnosis: Python refuses to ask whether a text string "38.5" is greater than the number 38.0—saving you from subtle alphabetical sorting bugs (where "9.0" > "38.5" would be True as text because '9' comes after '3'!).
Fix: Cast first: has_fever = float(raw_temp) >= 38.0.
2.4 Stage 4 — The Researcher’s Problem-Structuring Blueprint
Scenario: A community health worker’s mobile form submits a single patient intake row where every field arrives as raw text: raw_weight_kg = "64.5", raw_height_cm = "168", raw_hb = "10.4", raw_temp_c = "38.2". Parse all values into proper numeric types, compute BMI (rounded to 2 decimals), flag fever (\(\ge 38.0^\circ\text{C}\)) and maternal anemia (\(< 11.0\text{ g/dL}\)) as booleans, and set needs_priority_review to True if either condition is triggered.
2.4.1 Blueprint (Completed on Paper Before Coding)
- Inputs & Units: Four raw
strfields representing kg, cm, g/dL, and °C. - Edge Cases & Traps:
- Comparing uncast strings to thresholds raises
TypeError. - Height arrives in centimeters (
"168") and must be divided by100before squaring. - Use Python’s logical
oroperator soneeds_priority_reviewisTruewhenever at least one clinical flag isTrue.
- Comparing uncast strings to thresholds raises
- Step-by-Step Algorithm:
- Cast all four raw strings using
float(...). - Convert
height_cm\(\rightarrow\)height_m = float(raw_height_cm) / 100. - Compute
bmi = round(weight_kg / (height_m ** 2), 2). - Compute
is_febrile = temp_c >= 38.0,is_anemic = hb_g_dl < 11.0, andneeds_priority_review = is_febrile or is_anemic.
- Cast all four raw strings using
- Verification Plan (Hand Check):
- \(1.68^2 = 2.8224\); \(64.5 / 2.8224 = 22.8528\ldots \approx 22.85\).
- \(38.2 \ge 38.0 \Rightarrow \text{True}\); \(10.4 < 11.0 \Rightarrow \text{True}\); \(\text{True or True} \Rightarrow \text{True}\).
2.4.2 Clean Python Implementation (Verbatim in 01_walkthrough.ipynb)
# Step 1: Raw CSV-like string inputs
raw_weight_kg = "64.5"
raw_height_cm = "168"
raw_hb = "10.4"
raw_temp_c = "38.2"
# Step 2: Explicit type normalization & unit conversion
weight_kg = float(raw_weight_kg)
height_m = float(raw_height_cm) / 100
hb_g_dl = float(raw_hb)
temp_c = float(raw_temp_c)
# Step 3: Derived metrics & boolean clinical screening flags
bmi = round(weight_kg / (height_m ** 2), 2)
is_febrile = temp_c >= 38.0
is_anemic = hb_g_dl < 11.0
needs_priority_review = is_febrile or is_anemic
# Step 4: Clean clinical triage summary
print(f"BMI: {bmi} kg/m^2 | Febrile: {is_febrile} | Anemic: {is_anemic}")
print(f"Priority Review Needed: {needs_priority_review}")Output:
2.5 Stage 5 — Guided “To-Do” Practice Labs
Open part_01_python_foundations/ch02_variables_types/02_guided_todos.ipynb in Google Colab, run Step 0 to activate sharukh-tutor, and solve all six exercises below:
- To-Do 2.1 (Warm-Up — Casting Raw Survey Strings): Convert
raw_age = "34",raw_hb = "11.8",raw_visits = "4"intoage_int(int),hb_float(float), andvisits_int(int). - To-Do 2.2 (Rebinding — Augmented Assignment Queue): Start
waiting_patients = 12, then apply+= 7,-= 4, and+= 5sowaiting_patientsequals20. - To-Do 2.3 (Boolean Screening — Clinical Triage Flags): Given
temp_c = 37.2,sbp_mmhg = 146,hb_g_dl = 10.2, computehas_fever(\(\ge 38.0\)),is_hypertensive(\(\ge 140\)), andis_anemic(\(< 11.0\)). - To-Do 2.4 (Real Survey Trap — Two-Step Decimal-String Cast): Convert
raw_months = "24.0"cleanly into an exact integermonths_int(24) without triggeringValueError. - To-Do 2.5 (Scientific Precision —
==vs.math.isclose()): Givendose_a = 0.1,dose_b = 0.2,target_dose = 0.3, computeexact_eq = (dose_a + dose_b) == target_doseandsafe_eq = math.isclose(dose_a + dose_b, target_dose). - To-Do 2.6 (Stretch Clinical Math — Pediatric Dosage): For
weight_kg = 18.5,15mg/kg per dose,3doses/day, and250mg tablets, computedaily_mg,exact_tablets(rounded to 2 decimals), anddose_is_float = isinstance(daily_mg, float).
2.6 Stage 6 — Concept & Execution Trace Test (No Computer Allowed!)
Test your understanding on paper before checking Stage 8:
Memory Model: After running
a = 500,b = a,a = 600, what exact value doesbpoint to—500or600? Why?Valid Identifiers: Which of these variable names will trigger a
SyntaxError:sbp_1,_temp,2nd_dose,total_cases,class?Type Prediction: What does
type(10 / 2)return? What abouttype(10 // 2)?Truncation vs. Rounding: What does
int(9.99)evaluate to? What doesround(9.99)evaluate to?Spot the Error: What exact exception does
int("18.0")raise, and how do you fix it in one expression?Silent String Comparison Trap: What does
"9" > "38"evaluate to in Python, and why is it dangerous when filtering ages?Execution Trace Table: Trace
stockafter each line:Missing Value Discipline: Why is
hb_g_dl = Nonesafer thanhb_g_dl = 0for a patient whose blood test was not performed?Float Equality: Why does
(0.1 + 0.2) == 0.3evaluate toFalse, and what function should replace==?Boolean Type Check: What is the difference between
is_anemic = Trueandis_anemic = "True"?
2.7 Stage 7 — Chapter 2 Hands-On Coding Exam (100 Points)
Open part_01_python_foundations/ch02_variables_types/03_coding_exam.ipynb, complete all three tasks, and run tutor.check_exam("ch02", exam_submissions) to grade your work out of 100 points.
2.7.1 Exam Task 2.1 (30 Points): DHS Survey Record Normalization
Given raw survey strings raw_weight_kg = "54.6", raw_height_cm = "156.0", and raw_anc_visits = "4.0", compute with exact types:
weight_kg(float)height_m(float, converted from centimeters to meters)anc_visits(int, parsed cleanly from"4.0")bmi(float, rounded to 2 decimal places)
2.7.2 Exam Task 2.2 (35 Points): 4-Day Outbreak Accumulator & Threshold Alert
A district of population = 250000 records 4 days of dengue cases: 18, 27, 34, 21. Compute:
total_cases(int, accumulated starting from0using+=)mean_daily_cases(total_cases / 4)incidence_per_100k((total_cases / population) * 100000)outbreak_alert(bool:Trueifincidence_per_100k >= 35.0)
2.7.3 Exam Task 2.3 (35 Points): Biostatistics \(2 \times 2\) Table Odds & Odds Ratio
In a maternal anemia study comparing mothers without iron supplementation (Exposed: a = 45 anemic, b = 105 non-anemic) against iron-supplemented mothers (Unexposed: c = 20 anemic, d = 130 non-anemic), compute:
odds_exposed: \(\frac{a}{b}\) rounded to 4 decimal placesodds_unexposed: \(\frac{c}{d}\) rounded to 4 decimal placesodds_ratio: exact cross-product \(\frac{a \times d}{b \times c}\) rounded to 2 decimal placeselevated_risk: booleanodds_ratio > 1.0
2.8 Stage 8 — Complete Annotated Solutions & Debugging Post-Mortem
2.8.1 A. Concept & Execution Trace Test Key
bstill points to500! Rebindinga = 600peelsa’s sticky note off500and moves it to a new object600, leavingbuntouched on500.2nd_dose(starts with a digit) andclass(reserved Python keyword) triggerSyntaxError.type(10 / 2)is<class 'float'>(5.0);type(10 // 2)is<class 'int'>(5).int(9.99)is9(truncation);round(9.99)is10.- Raises
ValueError; fix withint(float("18.0")). - Evaluates to
True! Strings compare character-by-character alphabetically ('9'>'3'), so sorting uncast age strings puts a 9-year-old as “older” than a 38-year-old. 100\(\rightarrow\)65\(\rightarrow\)130\(\rightarrow\)140.0is a numeric value that silently corrupts means, minimums, and regression models;Noneexplicitly signals missingness.0.1and0.2are repeating binary fractions in IEEE-754 base-2 (0.1 + 0.2 == 0.30000000000000004); replace==withmath.isclose(...).Trueis a genuinebool;"True"is a 4-character textstr(even"False"is truthy when evaluated as a string!).
2.8.2 B. Annotated Solutions — Guided To-Dos (Mirrored in 02_guided_todos_SOLUTION.ipynb)
import math
# To-Do 2.1
age_int = int("34") # 34 (int)
hb_float = float("11.8") # 11.8 (float)
visits_int = int("4") # 4 (int)
# To-Do 2.2
waiting_patients = 12
waiting_patients += 7 # 19
waiting_patients -= 4 # 15
waiting_patients += 5 # 20
# To-Do 2.3
temp_c, sbp_mmhg, hb_g_dl = 37.2, 146, 10.2
has_fever = temp_c >= 38.0 # False
is_hypertensive = sbp_mmhg >= 140 # True
is_anemic = hb_g_dl < 11.0 # True
# To-Do 2.4
months_int = int(float("24.0")) # 24 (int)
# To-Do 2.5
dose_a, dose_b, target_dose = 0.1, 0.2, 0.3
exact_eq = (dose_a + dose_b) == target_dose # False
safe_eq = math.isclose(dose_a + dose_b, target_dose) # True
# To-Do 2.6
daily_mg = 18.5 * 15 * 3 # 832.5
exact_tablets = round(daily_mg / 250, 2) # 3.33
dose_is_float = isinstance(daily_mg, float) # True2.8.3 C. Annotated Solutions — Chapter 2 Coding Exam (Mirrored in 03_coding_exam_SOLUTION.ipynb)
# Exam Task 2.1 (30 pts)
weight_kg = float("54.6") # 54.6
height_m = float("156.0") / 100 # 1.56
anc_visits = int(float("4.0")) # 4
bmi = round(weight_kg / (height_m ** 2), 2) # 22.44
# Exam Task 2.2 (35 pts)
population = 250000
total_cases = 0
total_cases += 18
total_cases += 27
total_cases += 34
total_cases += 21 # 100
mean_daily_cases = total_cases / 4 # 25.0
incidence_per_100k = (total_cases / population) * 100000 # 40.0
outbreak_alert = incidence_per_100k >= 35.0 # True
# Exam Task 2.3 (35 pts)
a, b = 45, 105
c, d = 20, 130
odds_exposed = round(a / b, 4) # 0.4286
odds_unexposed = round(c / d, 4) # 0.1538
odds_ratio = round((a * d) / (b * c), 2) # 2.79
elevated_risk = odds_ratio > 1.0 # True2.8.4 D. Debugging Post-Mortem: Top 3 Chapter 2 Failure Modes
- Missing Denominator Parentheses in the Odds Ratio (
(a * d) / b * c): Because/and*share equal precedence and evaluate left-to-right, omitting parentheses around(b * c)multiplies bycinstead of dividing—producing an impossible Odds Ratio of 1,114.29 instead of 2.79! - Intermediate Rounding Drift: Computing
odds_ratio = round(odds_exposed / odds_unexposed, 2)divides two already-rounded numbers (0.4286 / 0.1538 = 2.7867...). Always compute statistical ratios from full-precision inputs(a * d) / (b * c)and round only once at final presentation. - Confusing Odds with Probability (Risk): In Task 2.3, the risk (prevalence) of anemia in the exposed group is \(\frac{45}{45 + 105} = 0.30\) (30%), whereas the odds of anemia are \(\frac{45}{105} = 0.4286\). Mastering this exact distinction now makes logistic regression coefficients in Chapter 23 effortless!


