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.
| 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