Advanced Python
Concurrency, performance, metaprogramming, graphs, dynamic programming and internals.
-
Lesson 14 min read
Python graphs: BFS and DFS explained, with the shortest path
How to store a graph in Python as an adjacency list, find the shortest path with breadth first search and a deque, and when depth first search is the better walk. One small function with its real output.
-
Lesson 27 min read
Python Dijkstra's algorithm with heapq: the cheapest path in a weighted graph
How to find the cheapest path through a weighted graph in Python with Dijkstra's algorithm and heapq, how relaxation works, why a negative weight gives a wrong answer, and how Bellman-Ford fixes it. Two short functions with their real output.
-
Lesson 36 min read
Python dynamic programming: memoisation, tabulation and lru_cache
How dynamic programming works in Python: why recursive Fibonacci repeats work, memoisation with lru_cache, tabulation with a table, then 0/1 knapsack, the longest increasing subsequence and edit distance, each with real output.
-
Lesson 410 min read
Python trie and union find: prefix search, autocomplete and Kruskal
How to build a trie in Python for prefix search and autocomplete, then union find with path compression and union by size, and use it to run Kruskal's algorithm for a minimum spanning tree. Four short programs with their real output.
-
Lesson 59 min read
Python asyncio: async, await, gather and timeouts explained
How Python asyncio works: the event loop, async def and await, running many things at once with create_task and gather, putting a limit on the wait with timeout, keeping tasks safe with TaskGroup, why a blocking call freezes everything, and the Python 3.14 tools that show running tasks. Three short programs with their real output.
-
Lesson 67 min read
Python threads vs processes: the GIL, locks and free threading
How Python threads, processes and subinterpreters differ: what the GIL is, why threads help with waiting but not calculating, how ThreadPoolExecutor and ProcessPoolExecutor compare, how a Lock stops a race condition, and what the Python 3.14 free-threaded build changes. Three short programs with their real output.
-
Lesson 78 min read
Python memory management: reference counting, garbage collection and __slots__
How Python frees memory: reference counting with sys.getrefcount, reference cycles and the gc module, why sys.getsizeof is shallow and tracemalloc is not, what __slots__ saves, how small integers and interned strings are shared, and how weakref works. Four short programs with their real output.
-
Lesson 87 min read
Python descriptors and metaclasses explained: property, __set_name__ and __init_subclass__
How Python descriptors work: __get__ and __set__ on the class, how property is built from one, a reusable Positive descriptor with __set_name__, data versus non data descriptors, __init_subclass__ for a plugin registry, and when a metaclass is the right tool. Five short programs with their real output.
-
Lesson 98 min read
Python import system explained: sys.modules, sys.path, importlib and circular imports
How the Python import system works: modules and packages, why a module runs only once, how sys.path decides where Python looks, relative imports, importlib, circular imports and how to fix them, and lazy import coming in Python 3.15. Four short programs with their real output.
-
Lesson 1010 min read
Python profiling and performance: cProfile, timeit and NumPy
How to profile Python code and make it faster: time small pieces with timeit, find the slow function with cProfile, read tottime and cumtime, fix the data structure first, use NumPy vectorisation, and what the sampling profiler in Python 3.15 and the experimental JIT in 3.14 change. Three short programs with their real output.
-
Lesson 118 min read
Python design patterns: strategy, observer, factory and singleton
How the classic design patterns look in Python: strategy as a function you pass in, observer as a list of functions, factory as a dict from a name to a function, and singleton as a module or functools.cache. Three short programs with their real output, and when the class version is still worth writing.
-
Lesson 127 min read
Python context managers and protocols: with, contextlib, Protocol
What the with statement really calls, how to write your own context manager as a class or with @contextmanager, what contextlib gives you for free, and how typing.Protocol matches a class by its methods instead of its parents. Three short programs with their real output.
-
Lesson 138 min read
Python template strings: t-strings in 3.14, Template and Interpolation
How a t-string differs from an f-string, what a Template object holds, and how to write a processor that escapes HTML or turns a query into placeholders. Three short programs with their real output.