Skip to content

All lessons

Area 00 // FoundationsLesson 09 / 14014 min read

Python for AI engineers

Almost all AI work happens in Python, so you need a clean setup (a virtual environment per project, installed with uv or pip), notebooks for experiments, and two libraries, numpy for fast maths and pandas for tables.

Think of it like this

Analogy

A chef keeps a separate, labelled box of ingredients for each dish, so the chilli for the curry never ends up in the cake. A virtual environment is that box: each project gets its own set of Python packages.

The notebook is the chef's tasting spoon: quick experiments, one bite at a time. The recipe card (a .py file) is what you write down once it works, so anyone can cook it again. numpy and pandas are the knives and chopping board you'll use for everything.

The problem it solves

Beginners usually install everything into one global Python. A month later, one project needs a newer library version, upgrading it breaks another project, and nobody can remember which versions made things work. Add API keys pasted into code and notebooks that only run if you click cells in a secret order, and you have code nobody else (including future you) can run.

A clean, boring setup avoids all of that, and it's the same setup used in the rest of this course.

How it works, step by step

  1. Install Python 3.11 or newer (python.org, or let uv install it for you).
  2. Create a project folder and a virtual environment inside it:
    • With uv (fast, recommended): uv venv creates .venv.
    • With plain Python: python -m venv .venv.
  3. Activate it so python and installs use that box: .venv\Scripts\activate on Windows, source .venv/bin/activate on macOS/Linux.
  4. Install packages: uv pip install numpy pandas or pip install numpy pandas (pip is Python's standard installer). Record them in pyproject.toml (with uv add numpy) or requirements.txt, so others get the same versions.
  5. Explore in a notebook. A Jupyter notebook (or VS Code's notebook view) runs code in cells and shows results inline: tables, charts, model outputs. Perfect for poking at data and prompts.
  6. Move working code into .py files with functions and tests once you'll reuse it.
  7. Use numpy for maths on arrays (vectors and matrices from lesson 6) and pandas for tables. A pandas table is a DataFrame: rows and named columns, like a spreadsheet you control with code.
  8. Keep secrets in environment variables. Put OPENAI_API_KEY=... in a .env file that's in .gitignore, or set it in your shell. Never in code.
Terminal: a fresh AI project with uv
uv init my-ai-project        # creates a folder with pyproject.toml
cd my-ai-project
uv add numpy pandas openai   # creates .venv and installs, recording versions
uv run python main.py        # runs inside the project's environment

See it in code

Install
pip install numpy pandas
Needs
Python 3.10+

The pandas and numpy moves you'll use constantly: load a table, look at it, group and summarise, then compute on a whole column at once (here, the cost of the tokens each ticket used, at an example price).

tickets.py
import io
import numpy as np
import pandas as pd
 
# A small CSV of support tickets (in real life: pd.read_csv("tickets.csv")).
csv = io.StringIO("""ticket,category,minutes_to_reply,tokens_used
1,billing,12,850
2,bug,45,2300
3,billing,8,600
4,account,20,1200
5,bug,30,1900
""")
df = pd.read_csv(csv)                 # a DataFrame: a table with named columns
 
print(df.head(3))                     # first rows, to eyeball the data
print("\naverage reply time:", df["minutes_to_reply"].mean(), "min")
 
# Group and summarise: the most common pandas move.
summary = df.groupby("category")["tokens_used"].agg(["count", "mean"])
print("\n", summary, sep="")
 
# numpy: fast maths on whole columns at once (no Python loops).
tokens = df["tokens_used"].to_numpy()
cost = tokens / 1_000_000 * 2.00      # e.g. $2.00 per million input tokens
print("\ntotal cost: $", round(float(np.sum(cost)), 4))
Output
   ticket category  minutes_to_reply  tokens_used
0       1  billing                12          850
1       2      bug                45         2300
2       3  billing                 8          600
 
average reply time: 23.0 min
 
          count    mean
category               
account       1  1200.0
billing       2   725.0
bug           2  2100.0
 
total cost: $ 0.0137

A real example

Example

An AI engineer's first week on a support-bot team

On day one you clone the repo and run uv sync: it creates the environment and installs the exact versions the team uses, so the app runs on the first try. You open a notebook, load last month's chat logs with pandas, and group them by topic to see which questions the bot gets wrong most often. You find that billing questions use the most tokens (and money).

You turn that analysis into a script, scripts/token_report.py, so it can run every week, and the team adds it to CI. Same tools as this lesson, nothing more.

The easy way

Start here

  1. Install uv (one command from its docs).
  2. uv init ai-course && cd ai-course && uv add numpy pandas scikit-learn
  3. Paste each lesson's snippet into a file and run it with uv run python file.py.

That's all you need for every Foundations lesson.

When you grow: the production setup

Teams pin exact versions with a lock file (uv.lock), run code in Docker for identical environments everywhere (lesson 109), format and lint with Ruff, type-check, test with pytest (lesson 140) and load config from environment variables validated with Pydantic (lesson 127).

Trade-offs

What you gain

  • Python has the biggest AI ecosystem: every model provider and library ships a Python SDK first.
  • Virtual environments make projects reproducible and isolated.
  • Notebooks make exploring data and prompts fast and visual.

What it costs you

  • Python is slower than compiled languages for raw loops (numpy and libraries hide most of this).
  • Packaging has many tools (pip, venv, conda, poetry, uv), which confuses beginners.
  • Notebooks can hide state and run-order bugs.

When not to use it

  • Your AI feature lives inside a web front end: TypeScript SDKs (OpenAI, Anthropic, Vercel AI SDK) may fit better. The ideas are the same.

Common mistakes

  • Installing everything globally with sudo pip install, then fighting version conflicts.
  • Forgetting to activate the environment, so packages install somewhere else.
  • Committing .venv or .env (with your API key) to Git. Add both to .gitignore.
  • Writing loops over numbers that numpy could do in one line, or loops over rows that pandas can groupby.
  • Shipping a notebook as "the app".

Interview questions

Try answering out loud first, then open the model answer.

How do you set up a reproducible Python environment for an AI project?

Model answer

One virtual environment per project, created with uv (or venv), dependencies declared in pyproject.toml with a lock file for exact versions, and a README command to recreate it (uv sync). Secrets come from environment variables, not code. For deployment I'd build the same environment into a Docker image so dev, CI and production match.

What are notebooks good for, and what are their risks?

Model answer

They're great for exploration: inspecting data, trying prompts, plotting results, sharing analysis. Risks: cells run out of order leave hidden state, so results may not reproduce; they're hard to diff, review and test; and outputs can leak secrets or customer data into Git. Move stable logic into modules with tests and keep notebooks thin.

Why are numpy and pandas fast compared with plain Python?

Model answer

They store data in contiguous typed arrays and run operations in compiled C or C++ code over whole arrays at once (vectorisation), instead of executing the Python interpreter for each element. A sum over a million numbers is one call into optimised code rather than a million Python-level additions.

Quiz

Pick an answer to see whether it’s right and why.

  1. 01Why use a virtual environment for each project?
  2. 02What is uv?
  3. 03When should you move code out of a notebook into a .py file?
  4. 04What's the main reason numpy is used instead of plain Python lists for maths?
  5. 05Which pandas call gives the average tokens per ticket category?
  6. 06Where should your OpenAI or Anthropic API key live?

0 of 6 answered

Key terms

Virtual environment
A private folder of Python packages for one project, so projects don't break each other's versions.
uv
A very fast Python package and project manager that creates environments, installs packages and locks versions.
pip
Python's standard package installer, e.g. pip install numpy.
Jupyter notebook
A document that mixes runnable code cells with their outputs (tables, charts, text). Great for exploring, fragile for production.
numpy
The Python library for fast maths on arrays of numbers (vectors and matrices).
pandas
The Python library for working with tables of data (DataFrames), for loading, filtering, grouping and summarising.
DataFrame
A pandas table with rows and named columns, like a spreadsheet controlled by code.

Before and after

Progress is saved in this browser only.