Python from First PrinciplesHow to use this guide · 20 of 20 written

Python from First Principles

Foundations · 20 posts1h 00m read

Python becomes easy to master once you see it as one idea applied everywhere: names point at objects, and every object's behavior comes from its type. With that idea, lists, functions, classes, NumPy arrays and PyTorch modules stop being separate topics.

How to use this guide

The guide has four layers, each building on the one before:

  1. The machine (Parts 1–3): what runs your code, what objects are, what each data structure costs.
  2. The language (Parts 4–10): control flow, functions, iteration, classes, errors, modules, types.
  3. The runtime (Parts 11–12): performance and concurrency, where AI code is won or lost.
  4. The AI stack (Parts 13–17): NumPy, data formats, PyTorch, LLM systems, tooling and testing.

Part 18 turns everything into questions you can ask of any code, including code an AI assistant wrote. Part 19 is a build-to-learn roadmap.

How to study each part:

  • Run every snippet in a REPL (python or ipython). Predict the output before you press Enter.
  • When a prediction is wrong, that is the lesson. Write down the rule that would have predicted it.
  • Use the three introspection tools constantly: type(x), dir(x), help(x). Python can explain itself.

The three questions that unlock most Python:

  1. What object does this name point to, and who else points to it?
  2. What type is it, and which special method does this syntax call on it?
  3. Where does the time go: the Python interpreter, or compiled code underneath?

Written for Python 3.12 or newer. Use the newest stable version your libraries support.