Chapter 0 — Basics of Python

Variables, control flow, and functions

Prof. Xuhu Wan

Chapter 0 · Introduction to Business Analytics

Basics of Python

The seven foundations every Python user must know cold.

Prof. Xuhu Wan

ISOM, HKUST Business School · Wan Academy · 2026 Edition

What This Chapter Covers

  1. Variables and arithmeticrevenue = 1_250_000
  2. Strings — text in quotes
  3. Boolean logic — yes/no decisions
  4. If / elif / else — branching
  5. For loops — repetition
  6. Functions — reusable logic
  7. Roundinground() and banker’s rounding

Tip

This chapter is the foundation. Read the full prose treatment in the book — Chapter 0.

Your First Line

The output appears immediately below the cell. Edit the line, click Run, and see what happens.

Variables and Arithmetic

Note

Underscores in numeric literals (1_250_000) are cosmetic — they don’t change the value, they just make large numbers readable.

Banker’s Rounding — A Gotcha

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.

If / Elif / Else

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.

For Loops

Or, more Pythonically:

Functions

Note

A function packages logic for reuse. Write it once, test it once, and reuse it from your backtester, dashboard, and execution engine.

Chapter Summary

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