Multi-page course · Cross-linked

Python for data & AI systems

A practical path from environments and idiomatic Python to the stacks used in data science, classical ML, deep learning with PyTorch, and production APIs with FastAPI. Use this hub to navigate four pages—each deepens a different slice of the ecosystem.

venv / pip typing NumPy / Pandas scikit-learn PyTorch FastAPI
4
Pages
Ecosystem
00

Learning track — pick your next page

Start here for environments and core language habits, then branch. Each page is self-contained but assumes you can run Python 3.10+ locally.

Prefer a linear path? Go Hub → Data & ML → PyTorch → FastAPI. If you already know Pandas, jump straight to PyTorch, then learn how to deploy models on the FastAPI page.
01

Environments & tooling

📦

Virtual environments & installs

Isolate dependencies per project — non-negotiable for ML stacks

toolingreproducibility

Never install PyTorch or Pandas into the system Python on your laptop. Use a venv (stdlib) or conda/mamba for scientific stacks, or uv for fast installs. Pin versions in requirements.txt or pyproject.toml for anything you ship.

bash
# Create and activate (Windows PowerShell)
python -m venv .venv
.\.venv\Scripts\Activate.ps1

# macOS / Linux
python3 -m venv .venv
source .venv/bin/activate

pip install --upgrade pip
pip install numpy pandas scikit-learn torch fastapi uvicorn
pip freeze > requirements.txt
Commit requirements.txt or lockfile; add .venv/ to .gitignore. For GPU PyTorch, follow the official install matrix (CUDA vs CPU).
02

Syntax, types & collections

🧩

Idioms you will see everywhere

List/dict comprehensions, unpacking, f-strings

basics
python
# Comprehensions — prefer over map/filter for readability
squares = [x * x for x in range(10) if x % 2 == 0]

# Dict & set comprehensions
word_lengths = {w: len(w) for w in ["ai", "ml", "de"]}

# Unpacking
first, *rest, last = [1, 2, 3, 4, 5]

# f-strings (Python 3.8+)
name, loss = "train", 0.42
print(f"epoch={name!r} loss={loss:.4f}")

For data work you will live in iterables and generators when files or streams are too large to load at once—see the FastAPI page for streaming responses.

Type hints & data classes

Readable APIs — pairs well with Pydantic on the FastAPI page

python
from dataclasses import dataclass
from typing import Optional, Sequence

@dataclass
class BatchConfig:
    batch_size: int = 32
    shuffle: bool = True
    drop_last: Optional[bool] = None

def moving_average(xs: Sequence[float], window: int) -> list[float]:
    """Hints document intent; mypy/pyright catch mistakes."""
    ...
Hints are optional at runtime. Libraries like Pydantic enforce validation when you opt in—essential for ML APIs.
03

Project layout & testing

🗂

Packages, src/ layout & pytest

How ML repos are usually organized

Typical layout
project/ pyproject.toml src/mypkg/ __init__.py train.py tests/test_train.py
Why tests matter in ML

Assert data transforms, metric computation, and inference code—models are hard to test end-to-end, but pure functions should be deterministic.

python
# tests/test_metrics.py — example pattern
import pytest

def accuracy(y_true, y_pred):
    return sum(a == b for a, b in zip(y_true, y_pred)) / len(y_true)

def test_accuracy_perfect():
    y_true = [0, 1, 1]
    y_pred = [0, 1, 1]
    assert accuracy(y_true, y_pred) == pytest.approx(1.0)
Next steps
Data & ML — NumPy, Pandas, scikit-learn
PyTorch & AI — deep learning core
FastAPI & engineering — ship models behind HTTP
Theory (visuals): ML & AI theory hub