Lesson 2.1foundation2 h 30 min

Python for ML, fast

The 20% of the language that writes 95% of ML code

Python is the language of machine learning because the heavy lifting happens in compiled libraries (NumPy, PyTorch) while Python glues them together readably. You do not need to be a Python expert; you need fluent control of a specific subset — containers, comprehensions, functions, classes, generators, typing — plus the discipline of isolated environments. Everything in 2.2 NumPy and vectorized thinking, 2.4 PyTorch I: tensors, autograd and the training loop and the training loops of 4.9 PyTorch II: real training pipelines is built from these pieces.

You'll be able to

  • Use lists, tuples, dicts and sets fluently, including comprehensions, unpacking and sorting with a key
  • Write functions with default/keyword arguments, closures and generators, and know when a generator saves memory
  • Define configuration objects with dataclasses and type hints, and read other people's typed code
  • Create and manage isolated environments with uv, and explain when to use conda instead
  • Debug a failing script with breakpoint() and read a traceback bottom-up

Key ideas

  1. ML Python is orchestration code: fluent use of lists/dicts/sets, comprehensions, enumerate/zip and sorted(key=) covers most of what you will write.
  2. Generators (yield) process data lazily one item at a time; data pipelines and batch iterators are generators.
  3. Closures capture variables from their defining scope; learning-rate schedules and callbacks are closures.
Show 4 more key ideas
  1. Dataclasses are the idiomatic container for configuration; type hints document shapes and intent without runtime cost.
  2. f-strings, pathlib and context managers remove whole categories of bugs (formatting, path separators, forgotten cleanup).
  3. One isolated environment per project; uv (pyproject.toml + uv.lock) makes it fast and reproducible, conda when you need non-Python binaries.
  4. Debug by reading tracebacks bottom-up, dropping into breakpoint(), and printing shapes.

Suggested path through this lesson