Skip to main content
Back to Python Coding

Python Coding · Doing It With AI

Directing AI to Write Python

The one idea: You get better analysis by directing the AI up front than by fixing its code afterward — give it your data's shape, its grain, and the exact question, then demand a plain-English plan before any code exists. Then verify what comes back with row counts, one hand-checked slice, and a full restart-and-run.
⚠️ The trap: If I hand the AI my data and say "analyze this and tell me something interesting," I've delegated the grunt work and kept the thinking. That feels right and it's wrong. You just delegated the judgment — the question, every definition, and the decision are yours; give the AI the exact question and demand a plain-English plan before any code exists.

You now read pandas well enough to catch the standard bugs — which means you can do something more valuable than auditing after the fact: you can direct the work. This lesson is the operating manual for getting an AI to write your analysis: what to feed it, what to demand before code exists, and the verification loop that turns "the AI said" into "I verified."

The context that determines the quality of the code

An AI writing analysis code without knowing your data's shape is guessing — column names, types, grain, all of it. Plausible guesses compile. Give it these five things and most guessing disappears:

1. A real sample: paste df.head(). Ten actual rows beat any prose description. The AI sees real column names, real formats ("$1,200", "2026-04-03"), real category values.

2. The types: paste df.info(). Now it knows revenue is object (needs cleaning before math), dates are strings, and…

Keep reading: Directing AI to Write Python

Unlock the full Python Coding track — every lesson, the AI tutor, and full mock assessments.

  • Full lesson content
  • AI tutor for this section
  • Practice questions