Why a focused crash course
Most Python tutorials spend weeks on material you do not need to start building with AI. The truth is that calling a modern AI API requires a small slice of the language: a few data types, loops, functions, and the ability to send an HTTP request and read a JSON response. This crash course teaches that slice directly, so you can go from zero to a working API call quickly and learn the rest on demand. Run every example yourself — reading code is not the same as writing it.
Setting up Python the right way
Install Python 3.10 or newer from python.org, then for each project create an isolated environment so your packages do not collide:
python -m venv .venv
source .venv/bin/activate # on Windows: .venv\Scripts\activate
pip install openai
The virtual environment is a folder that holds this project’s packages only. Activate it whenever you work on the project. This single habit prevents the most common beginner frustration — packages from one project breaking another.
The data types you will actually use
Three structures cover almost everything in AI work. Variables hold values
(model = "gpt-4o-mini"). Lists hold ordered collections
(messages = [m1, m2]). Dictionaries map keys to values
({"role": "user", "content": "Hi"}). API requests and responses are built
entirely from nested dictionaries and lists, so reading a response means chaining
access: data["choices"][0]["message"]["content"]. Spend your first hour
becoming fluent in those three and you can read most AI code.
Loops, functions, and JSON
A loop repeats work — iterating over a list of prompts, for example. A
function packages reusable logic so you can call ask(prompt) instead of
copying request code everywhere. JSON is the text format APIs speak; Python’s
json module and the requests/openai libraries convert between JSON text and
Python dictionaries automatically. Knowing how to loop over results and wrap your
call in a function is enough to build a real script.
Your first AI API call
With the openai package installed and your key set as an environment variable
(export OPENAI_API_KEY=sk-...), a complete first program is short:
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Say hello in one sentence."}],
)
print(resp.choices[0].message.content)
Never hard-code your key in the file — read it from the environment so you can share code safely. From here, everything else (loops over many prompts, saving results, building a web app) is a combination of the basics above. Use the lesson navigator below to step through each concept with its own runnable snippet.