Python for AI Developers
Python for AI is the practical slice of Python that AI engineers use every single day: creating isolated projects, shaping data with lists and dictionaries, reading documents from disk, calling model APIs over HTTP, and crunching vectors and tables with NumPy and pandas. This course teaches that slice properly — clear definitions, runnable code, real output — and finishes with a dataset-prep project you can reuse for RAG or fine-tuning.
What is “Python for AI”?
The subset of the Python language, standard library, and third-party packages that you need to load data, prepare it, send it to machine-learning models or LLM APIs, and process the results. It is not a different language — it is ordinary Python, focused on the parts AI work actually uses.
Most people who want to build with large language models (LLMs) get stuck in one of two places. Either they try to learn all of Python first — decorators, metaclasses, async internals — and never reach an AI project, or they copy notebook code they do not understand and get lost the moment something breaks. This course takes the middle path: you learn every concept you need for real AI engineering, explained from first principles, and nothing you do not.
By the end you will be comfortable reading and writing the kind of Python that sits behind chatbots, retrieval-augmented generation (RAG) pipelines, evaluation scripts, and agents: scripts that read files, clean text, call HTTP APIs, store results as JSON, and analyse numbers with NumPy and pandas.
Why Python is the language of AI
- The ecosystem lives here. PyTorch, TensorFlow, Hugging Face Transformers, scikit-learn, LangChain, LlamaIndex, and the official OpenAI and Anthropic SDKs are all Python-first. New research code is almost always published in Python.
- It reads like pseudo-code. Python's syntax is small and readable, so you spend your attention on the AI problem rather than on the language.
- It is a “glue” language. Python is excellent at connecting things — files, databases, web APIs, and fast numeric libraries written in C, C++, or CUDA. AI systems are mostly glue.
- Same language from notebook to production. You can prototype in a Jupyter notebook and move the same code into a script, an API server, or a scheduled job.
A 30-second taste
Here is a tiny script that already does real AI-style work: it cleans a messy piece of text, counts its words, and packages the result as a JSON record — exactly what you do before sending documents to an embedding model.
import json
raw = " Retrieval-Augmented Generation grounds answers in YOUR documents. "
clean = " ".join(raw.split()) # collapse all whitespace
record = {
"text": clean,
"words": len(clean.split()),
"lower": clean.lower(),
}
print(json.dumps(record, indent=2))
Do not worry if every line is not obvious yet. By the end of the Lists, dicts, and JSON lesson you will be able to write this from memory and explain each piece.
What you will learn
| Lesson | What you learn | Why it matters for AI |
|---|---|---|
| Setup, venv, and packages | Installing Python, virtual environments, pip, requirements.txt, .env secrets | Every AI project depends on dozens of packages; isolation keeps installs reproducible and keys safe. |
| Types, functions, and modules | Variables, numbers, strings, booleans, control flow, functions, type hints, exceptions, imports | Prompt builders, cleaners, and API clients are all just well-written functions. |
| Lists, dicts, and JSON | Lists, tuples, sets, dicts, comprehensions, batching, JSON and JSONL | Model APIs send and receive JSON; datasets are lists of dicts. |
| Files and paths | pathlib, reading/writing text, CSV, encodings, globbing, chunking | RAG starts by loading and splitting documents from disk. |
| HTTP requests and APIs | HTTP methods, status codes, headers, auth, timeouts, retries, calling an LLM API | Every hosted model is an HTTP API. |
| NumPy and pandas basics | Arrays, vectorisation, cosine similarity, top-k search, DataFrames, groupby | Embeddings are vectors; evaluation results are tables. |
| Project: prepare a dataset | A complete clean → chunk → dedupe → JSONL pipeline with a report | Data preparation is the first real job in almost every AI project. |
What you will build
- A clean project layout with a virtual environment, pinned dependencies, and secrets loaded from a
.envfile. - Reusable helper functions for normalising text, building prompts, and estimating token counts.
- Scripts that read folders of documents, split them into overlapping chunks, and save them as JSON Lines.
- A small, robust HTTP client with timeouts, retries, and an LLM chat-completion call.
- A NumPy semantic-search toy that ranks documents by cosine similarity.
- A final dataset-preparation pipeline with a pandas quality report.
Who this course is for
- Complete beginners who want to become AI engineers and need a focused path into Python.
- Developers from JavaScript, Java, C#, or Go who need to read and write AI code in Python quickly and correctly.
- Data analysts and students who know a little Python from notebooks and want to write proper scripts and modules.
Prerequisites
- A computer running macOS, Windows, or Linux, where you can install software.
- Basic comfort with a terminal: opening it, changing folders with
cd, and running a command. Each command is shown in full. - No previous Python experience is required. If you already know some, skim the definitions and focus on the AI-specific examples and callouts.
Tools you need
| Tool | Purpose | Cost |
|---|---|---|
| Python 3.11 or newer | The interpreter that runs your code | Free |
| A code editor (Cursor or VS Code) | Writing code with syntax highlighting, linting, and an integrated terminal | Free tier available |
| A terminal | Creating environments, installing packages, running scripts | Built in |
| An LLM API key (optional) | Only for the live model call in the HTTP lesson; everything else runs offline or against free test APIs | Pay-as-you-go |
How every lesson is structured
- Definitions first. Each new idea starts with a short, precise definition in a highlighted box.
- Explanation in plain English. What the concept is, how it works, and why AI engineers care.
- Runnable code. Every example is complete; copy it with the Copy button and run it.
- Expected output. The exact output appears under each runnable example so you can check your result.
- Watch out / Note boxes. The mistakes that actually bite people in production.
- Try it exercises and key takeaways. Practise, then lock in the main ideas.
How to study this course
- Type the code, do not just read it. Typing builds the muscle memory that reading never will.
- Break things on purpose. Change a value, delete a line, and read the error message. Errors are the fastest teacher in Python.
- Keep one project folder (we call it
ai-lab) and add each lesson's scripts to it. By the end you have a personal toolkit. - Finish the Try it boxes. They are short and directly practise the lesson's main skill.
Key terms you will meet
| Term | Meaning |
|---|---|
| Interpreter | The program (python) that reads your .py file and executes it line by line. |
| Script | A .py file you run directly, for example python prep.py. |
| Module | Any .py file whose functions and variables can be imported by other files. |
| Package | Installable, reusable code — such as requests or numpy — usually downloaded from PyPI with pip. |
| Virtual environment | A private folder of packages for one project, so projects do not interfere with each other. |
| API | Application Programming Interface — a defined way for programs to talk to each other, usually over HTTP with JSON. |
| JSON / JSONL | Text formats for structured data. JSONL stores one JSON object per line and is the standard format for AI datasets. |
| Vector / embedding | A list of numbers that represents the meaning of text; similar meanings produce similar vectors. |
| DataFrame | A pandas table with named columns, like a spreadsheet you can program. |
Lessons build on each other. If you are new to Python, go in order. If you are experienced, jump to HTTP requests and APIs or NumPy and pandas basics and come back to fill gaps.
Course syllabus
Work through the lessons below in order. Your progress, streak, and badges are saved in this browser as you mark lessons done.
Syllabus