Python from First PrinciplesHow to use this guide · 20 of 20 written
- How to use this guide
- 01What Python actually is
- 02Names, objects and references
- 03Built-in types and what they cost
- 04Control flow and expressions
- 05Functions in depth
- 06Iteration and generators
- 07Classes and the data model
- 08Context managers and exceptions
- 09Modules, packages, imports and environments
- 10Type hints and Pydantic
- 11The performance model
- 12Concurrency and parallelism
- 13NumPy: the array model everything else copies
- 14Data and serialization
- 15PyTorch mental model
- 16AI systems engineering in Python
- 17Tooling, debugging and testing
- 18Asking the right questions
- 19Roadmap and build-to-learn projects
- 20Glossary and resources
Python from First Principles
Foundations · 20 posts1h 00m read
Foundations20 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:
- The machine (Parts 1–3): what runs your code, what objects are, what each data structure costs.
- The language (Parts 4–10): control flow, functions, iteration, classes, errors, modules, types.
- The runtime (Parts 11–12): performance and concurrency, where AI code is won or lost.
- 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 (
pythonoripython). 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:
- What object does this name point to, and who else points to it?
- What type is it, and which special method does this syntax call on it?
- 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.