Python from scratch

From data types and iterators to decorators, metaclasses and asyncio — then NumPy, Bézier curves and the GPU.

Python is easy to start and surprisingly deep. This course follows the repository from the basics — what a name, a list and a loop really are — through the features that make Python feel like Python: dunder methods, properties, decorators, metaclasses and coroutines. It finishes with numerical work: rotating images with NumPy, fitting Bézier curves with a Bernstein matrix, and a first CUDA kernel written in Python with Numba.

Running each script is part of the lesson. Several of them print something unexpected — a stray None, a literal {self.__name__}, an empty row in a rotated image — and each surprise is explained where it happens.

Prerequisites

None for the first half. The numerical lessons use NumPy and a little of the linear algebra course.

Running the code

cd python_from_scratch
python3 -m venv .venv && source .venv/bin/activate
pip install numpy scipy matplotlib
python3 Python_basics_and_intermidiate/d_deepcopy.py

The Python Basics/ folder also holds two notebooks — an introduction to NumPy arrays from an image-processing course and a “zero to hero” practice notebook — that pair well with the first lessons. The asyncio example needs pip install aiohttp, and the GPU lesson needs an NVIDIA GPU with numba and cupy installed.

Lessons

  1. 1 Data types and operatorsSix built-in types, which of them can change, and the difference between equal and identical.
  2. 2 Iterables, iterators and generatorsWhat a for loop actually calls, and why the repository's MyRange can only be used once.
  3. 3 Dunder methods and operator overloadingHow len(), [], + and < find their way to methods you write yourself.
  4. 4 References and copiesNames point at objects. Assignment, copy.copy and copy.deepcopy differ only in how many objects they create.
  5. 5 Getters, setters and @propertyAdding validation to an attribute without changing a single line of the code that uses it.
  6. 6 DecoratorsFunctions that take a function and return a better one — and where the stray None in the output comes from.
  7. 7 MetaclassesClasses are objects too, and a metaclass decides what happens when you call one.
  8. 8 Concurrency with asyncioOne thread, many tasks: how awaiting overlaps waits, turning ten seconds into five.
  9. 9 Command-line tools with argparseTurning a script into a tool with options, defaults, types and a free --help page.
  10. 10 Rotating an image with NumPyRotating pixels by hand exposes two classic bugs — an off-by-half centre and holes from forward mapping.
  11. 11 Bézier curves and the Bernstein matrixSmooth curves from a handful of control points, built by repeated interpolation, and fitted to data with one least-squares solve.
  12. 12 GPU kernels in Python with NumbaA tiled matrix multiplication with shared memory, written in Python — and three details that decide whether it is correct and how fast it looks.

Browse this course's code on GitHub →