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.

§1 — Variables and Arithmetic

Variables and Arithmetic

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.

§2 — Logic and Flow Control

Logic and Flow Control

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:

§3 — Functions

Functions

Functions

Note

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

§4 — Worked Example

Worked Example

Worked Example: HK$10K Summer Savings

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.

§5 — Chapter Wrap-up

Chapter Wrap-up

Working with an AI Copilot

  • Every analyst now works alongside ChatGPT, Claude, or Copilot
  • Writing code from scratch is no longer the differentiator
  • The skill is verifying the AI’s output — it will invent methods, swap arguments, divide by the wrong column
  • A good prompt gives the AI three things: your code, the error, and what you expected
# Bad: AI guesses what you wanted
"fix this code"
# 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?"
  1. Always run AI code yourself before pasting it anywhere
  2. Ask the AI to explain why it works — if you can’t rephrase it, you don’t own it
  3. Never trust an AI to do statistics it can’t show its working for

Mistakes Library: NASA Mars Climate Orbiter (1999)

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:

  • Put the unit in the namerevenue_hkd, not revenue; distance_km, not distance.
  • Audit units when reading code you didn’t write.

Decision Memo — Your First Analyst Output

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.

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