0.5 Why Python runs AI, and where it does not belong
Python is slow. A plain Python loop is often 30–100× slower than the same loop in Go or Rust. And yet nearly every model you will use was trained from a Python program. Both statements are true because of one design:
your Python code ~1% of the run time: "what to compute"
─────────────────────────────────────────────────────────────────────
NumPy / PyTorch / JAX C, C++, CUDA, Triton kernels: "computing it"
BLAS, cuDNN, NCCL ~99% of the run time, on all cores and the GPU
When you write loss = model(x).cross_entropy(y), Python spends a few
microseconds deciding which kernels to launch. The GPU then spends
milliseconds doing billions of multiply-adds. Python's speed stops
mattering when each Python line triggers that much native work. This is
why the rules for fast Python are "keep the loops in the library"
(vectorize, Ch 31 and 53) and "never loop over individual numbers in Python
when a library call can do the whole array".
Python won data science and AI because of:
- Readable code close to the maths.
y = W @ x + bis the equation. - The REPL and notebooks. Data work is exploratory: load, look, plot, change one thing, look again.
- A foreign-function interface that C and C++ authors liked. NumPy (2006) wrapped decades of Fortran and C numerical code; every later library built on NumPy's array.
- Network effects. Once scikit-learn, PyTorch, and Hugging Face were Python-first, the papers, tutorials, and hiring followed.
Where Python does not belong, or needs care:
| Situation | Problem | What teams do |
|---|---|---|
| Low-latency network services at high request rates | interpreter overhead per request, GIL (Ch 27), memory per process | Go or Rust for the hot path; Python behind it or offline |
| Single-binary CLI tools shipped to many machines | needs an interpreter and dependencies | Go/Rust, or a container, or a tool like uv tool / PyInstaller |
| Model inference in production at scale | Python adds latency and complexity around the kernels | serving engines written in C++/Rust/Go (llama.cpp, vLLM's core, TensorRT-LLM, Triton) with thin Python or none |
| Mobile and browser | no CPython | export the model (ONNX, GGUF, Core ML) and run it natively |
| Anything where a type error must never reach production | dynamic typing | strict type checking (Ch 18), tests (Ch 20), or a compiled language |
A common split, and the one this Wiki's projects use: Python offline, for data extraction, cleaning, training, evaluation, and export. Go or Rust online, for the services, the agent harness, and serving the exported model. You will see this split in Ch 60–61, and the AI guide's distillation project follows it: train in Python, run somewhere else.