Real Python (@realpython) header
Real Python (@realpython) profile picture

Helping Python developers build with AI: apps, agents, models, and AI-assisted coding. Expert-reviewed tutorials and courses, read by 1M+ developers a month.

Joined August 2012
5. You can design a quantum circuit on your laptop with Qiskit. qc.h(0) puts a qubit into an equal superposition of 0 and 1, and qc.measure(0, 0) reads it into a classical bit. Run the circuit 10,000 times on a simulator and roughly half the results are 0, half 1.
1
351
1. Shor's algorithm factors numbers by finding a repeating pattern. Take powers of 7 and keep the remainder after dividing by 15: 7, 4, 13, 1, 7, 4, 13, 1 The period, 4, gives you the factors 3 and 5. Factoring a 617-digit number would take classical methods astronomical time.
1
46
2. A tool's docstring is the description the LLM reads when it decides whether to call that tool. Register a function with @agent.tool_plain and skip the docstring, and its description is None. The LLM gets an empty description, so it has only the name and parameters to go on.
1
2
64
1. Pass a Pydantic model like CityInfo as output_type, and result.output is a CityInfo object with population as a real int. If the LLM's JSON doesn't fit CityInfo, Pydantic raises a ValidationError and Pydantic AI asks the LLM again. You write no regex and no string splitting.
1
3
82
5. bytes and str share most of their methods, but they don't mix. Call .replace() on a bytes object with str arguments and you get: TypeError: a bytes-like object is required, not 'str' Pass bytes, as in raw.replace(b"y", b"o"), or decode first and work with the str.
1
1
416
3. Two settings avoid the crash and still show you where the data broke. errors="replace" puts a � where each bad byte was. errors="backslashreplace" writes out the byte itself, like \xe9, so you can see exactly which byte failed.
1
23
Files, databases and APIs often hand you bytes. .decode() makes them text, until a byte doesn't fit: 1. The default that crashes 2. errors="ignore" dropping data, no error 3. Two settings that mark bad bytes 4. Which one to use when 5. A bytes method that rejects a str 🧵
2
1
8
1,855
This pytest test catches the bug. It feeds the pet 1, 10 and 15 times and expects 1, 10 and 10. Run it before you ask the agent anything, and make sure it fails. A test that passes on buggy code gives false confidence, and gives the agent nothing to check its fix against.
1
2
133
This alien pet's fed_level should stop at 10. Feed it 12 times and it reads 11, so is_happy() is False for good. The bug is one character. This thread walks through handing it to an AI coding agent like Claude Code or Copilot, then checking the fix 🧵
15
1
15
2,788
3. Your system prompt goes in the system= argument of client.messages.create(), next to model and max_tokens. Send it as the first entry in messages with "role": "system" and the API answers with a BadRequestError. Set it once in system= and it covers the whole conversation.
1
37