Module 2 — Python for Quants

Python Building Blocks for Quants

Pandas, NumPy, and the data-wrangling muscle memory you'll use every day.

Learning objectives

  • Use lists, dicts, and tuples idiomatically.
  • Understand when to reach for NumPy vs pure Python.
  • Write a small price-bar data class.

TEXT

Pick the right container

Lists are ordered and mutable — use them for sequences (returns, signals). Dicts map keys to values — use them for ticker→price lookups, parameter sets. Tuples are immutable and hashable — use them as composite keys ((date, ticker)). Sets are unordered, no duplicates — use them for universes (S&P 500 membership).

CODE

A minimal bar

from dataclasses import dataclass

@dataclass(frozen=True)
class Bar:
    date: str
    open: float
    high: float
    low: float
    close: float
    volume: int

bars = [Bar('2025-01-02', 100, 102, 99, 101, 1_200_000)]
for b in bars:
    print(b.close, b.high - b.low)

TEXT

When to switch to NumPy

Once you start doing arithmetic across hundreds of names, pure Python becomes 10–100x too slow. Vectorise: NumPy arrays let you compute returns for an entire panel in one operation, no for-loop.