Variables, control flow, and functions
The seven foundations every Python user must know cold.
Prof. Xuhu Wan
ISOM, HKUST Business School · Wan Academy · 2026 Edition
revenue = 1_250_000round() and banker’s roundingTip
This chapter is the foundation. Read the full prose treatment in the book — Chapter 0.
The output appears immediately below the cell. Edit the line, click Run, and see what happens.
Note
Underscores in numeric literals (1_250_000) are cosmetic — they don’t change the value, they just make large numbers readable.
Python uses round-half-to-even (IEEE 754 default) — over thousands of rounded numbers it removes the upward bias that “always round up” introduces. This is the standard in banking and statistics.
Important
Each condition is checked in order; the first true branch runs and the rest are skipped. The else catches everything that wasn’t matched above.
Or, more Pythonically:
Note
A function packages logic for reuse. Write it once, test it once, and reuse it from your backtester, dashboard, and execution engine.
You put HK$10,000 of summer-internship savings into a tracker fund at 7% a year. How much on graduation day?
Note
Closed-form and the loop agree to the cent: HK$13,107.96 — a 31% gain over four years.
# Good: AI has code + symptom + goal
"Here is my code: ...
Error: TypeError: unsupported operand type(s) for +: 'int' and 'str'.
I expected total revenue as an integer. What's wrong?"Warning
On 23 Sep 1999 NASA lost the Mars Climate Orbiter — a US$327M probe — burned up entering the Martian atmosphere.
Cause: a units mismatch. Lockheed Martin supplied thrust in pound-force seconds; NASA’s navigation software expected newton seconds. Both numbers looked sensible; neither variable carried the unit.
Python won’t save you. A variable named revenue says nothing about HKD, USD, or RMB. Two rules:
revenue_hkd, not revenue; distance_km, not distance.Every analyst’s job ends with a one-page memo, not a chart — this template recurs through the rest of the course.
To: Future Me
From: A Year-2 ISOM 2600 student
Subject: Park summer savings in a tracker fund until graduation
Date: 2026-05-16
Recommendation: Deposit HK$10,000 into a broad-market tracker fund; hold untouched for 4 years.
Evidence: At 7% annual return, terminal value = HK$13,107.96 — a 31% gain on principal.
Caveats: Assumes a constant 7% return; realised equity returns are volatile and can be sharply negative.
Next step: Re-run with 1-year and 10-year horizons to bracket the sensitivity to the holding period.
| Construct | Use |
|---|---|
= |
Assign a variable |
+ - * / // % |
Arithmetic |
'text' |
String literal |
True / False |
Boolean values |
if / elif / else |
Decisions |
for x in seq: |
Repetition |
def f(x): |
Define a function |
Next: Chapter 1 — Python Essentials (lists, pandas Series, NumPy arrays, SciPy stats).
Prof. Xuhu Wan · HKUST ISOM · Introduction to Business Analytics