Build garagePlay → read → next · ~8 min

Tutorials · Chapter D (4/4) · ~8 min

Python only what you need

Play → read → next

Write and run the small Python you’ll reuse in every AI lab ahead.

Code Lab

Python only what you need

Run the starter, then edit it. Print the number of topics.

Recap

What you just did

You wrote working Python, not lecture notes. You can point at the editor and say: “That’s my code. It ran.” Variables, a short loop, and a function that returns something useful — that’s the core stack for every lab after this.

Teach

How it works

Python for AI builders is a short list of moves:

scores = [0.2, 0.9, 0.4]
best = max(scores)

def label(score):
    return "yes" if score >= 0.5 else "no"

print(best, label(best))
  1. Names hold data (scores, best)
  2. Loops / list tricks walk many examples fast
  3. Functions package a repeatable decision
  4. Print / return prove the program did something

Mental model: code is a recipe. Inputs go in, steps run in order, you show the plate (output).

Use it

When you'd use this

  • Cleaning a list of labels before training
  • Scoring predictions and printing the best one
  • Wrapping “if this score, say yes” so you can reuse it in a model lab

Watch out

Watch out

Don’t copy a huge script you’ll never reread. Prefer five clear lines that run over fifty clever ones that don’t. Also: indentation matters in Python — a misaligned block fails loud, which is good.

Try next

Try this next

Change the threshold in label from 0.5 to 0.8. Rerun. Tell a friend what changed in plain English.