underlay
module 014–6h

Python

The language of the AI ecosystem

Every serious AI library — transformers, vLLM, LangChain, FastAPI — is Python. When your agent writes you a script, it's usually Python. You don't need to become a Python guru; you need to read it confidently and write small programs when the agent is overkill.

Here's the honest minimum that unlocks everything: collections, functions, errors, and how to run code in an environment that doesn't destroy your system.

Your first real Python program: the tokenizer demo

This module's favorite demo uses the actual OpenAI tokenizer. It converts text to the integer IDs an LLM processes. Watch the output — it will explain a mystery you'll meet again in module 7:

bashinstall and run — this works on any machine with Python
python3 -m venv .venv        # isolated environment (never pip install globally)
source .venv/bin/activate    # enter it
pip install tiktoken         # the real OpenAI tokenizer library
pythondemo.py — tokenizing with the real thing
import tiktoken

enc = tiktoken.get_encoding("o200k_base")   # GPT-4o/GPT-5 family tokenizer

for text in ["Hello world", "Hello, world!", "strawberry"]:
    tokens = enc.encode(text)
    pieces = [enc.decode_single_token_bytes(t).decode() for t in tokens]
    print(f"{text!r:20} -> {len(tokens)} tokens: {pieces}")

# 'Hello world'      -> 2 tokens: ['Hello', ' world']
# 'Hello, world!'    -> 4 tokens: ['Hello', ',', ' world', '!']
# 'strawberry'       -> 3 tokens: ['str', 'aw', 'berry']   <- the 3-r mystery

Read it line by line: import the library, grab the same tokenizer the GPT-5 models use, encode three strings, print what came out. That's it — you just ran the exact code that explains why LLMs miscount letters. Keep this script; you'll extend it in module 7.

The playground: pure Python, runs right here

tiktoken needs a pip install, so it can't run in this browser sandbox — but everything else can. Every Python block with a run button executes in a real sandbox. Change the comprehension, hit run, break it on purpose — that's learning:

pythonedit it. run it. break it.
executes in a sandbox
devices = ["iPhone 15", "Pixel 9", "Galaxy S24", "iPhone 13"]

# comprehension: build a new dict from an old list
prices = {d: len(d) * 10 for d in devices}

for d, p in prices.items():
    print(f"{d}: £{p}")

# try adding an if filter, or a nested loop. then run again.

Errors are information, and tests are confidence

Python errors end in a traceback — the full path of what failed, from the last line back to where it started. Read it bottom-up: the LAST line is the actual error; everything above is the journey. And pytest is how professionals prove code works, which matters doubly when an AI wrote the code:

pythontest_demo.py — prove the tokenizer math
from demo import token_count

def test_hello_world_is_two_tokens():
    assert token_count("Hello world") == 2

def test_strawberry_is_three_tokens():
    assert token_count("strawberry") == 3
bashrun it — green is your new favorite color
pip install pytest
pytest -q                # -> 2 passed

# schedule it on your server (the real check)
crontab -e               # add:
# 0 9 * * * cd /home/you/demo && .venv/bin/python demo.py >> log.txt 2>&1

test yourself

01Why do you create a venv instead of pip-installing globally?

02The last line of a Python traceback tells you:

03What does this print: [x**2 for x in range(4)]

the real check

A pytest suite goes green. A script runs as a cron job on your VPS.

0 stamps earned·streak: 0 days

Signed out: progress lives in this browser. to carry it everywhere.

vibe-coder payoff

The whole AI ecosystem is Python. If you can read it, you can steer it.