AI Tools8 min read· May 9, 2026

Python for AI Beginners: Write Your First AI Script in 10 Minutes

Complete beginner guide to Python for AI in 2026. Install Python, call the ChatGPT API, and run your first AI script in 10 minutes — no coding experience needed.

Python for AI Beginners: Write Your First AI Script in 10 Minutes

Python is the language of AI. Every tutorial, every API, every open-source model — they all assume you know at least a little Python. The good news is you don't need to be a developer to use it. You just need to know enough to run a script.

This guide skips all the theory. By the end, you'll have Python installed, connected to an AI model, and running your first real AI script. It takes about 10 minutes.

Before starting, make sure you're comfortable opening a terminal window. If not, read our terminal beginners guide first — it takes 5 minutes and covers everything you'll need here.


What You'll Build

By the end of this guide, you'll have a script that:

  • Sends a question to an AI model
  • Gets a response back
  • Prints it in your terminal

That's it. Simple, but it's the foundation for every AI tool you'll ever build — chatbots, summarizers, content generators, price checkers, anything.


Step 1 — Install Python

Go to python.org/downloads and download the latest Python 3.x release.

On Windows: Run the installer. On the first screen, check the box that says "Add Python to PATH" before clicking Install. This is the most common beginner mistake — if you skip it, nothing will work from the terminal.

On Mac: The installer works the same way. Alternatively, if you have Homebrew installed:

brew install python

Verify it worked — open your terminal and run:

python --version

You should see something like Python 3.12.x. If you see an error, you likely forgot the "Add to PATH" checkbox on Windows — rerun the installer and check it.


Step 2 — Install the OpenAI Library

Python uses a package manager called pip to install libraries. You need just one command:

pip install openai

This downloads the official OpenAI library — the same one developers use to build apps with ChatGPT.

If pip isn't found, try pip3 instead:

pip3 install openai

Step 3 — Get Your OpenAI API Key

An API key is how Python identifies itself to OpenAI's servers. Think of it as a password that gets attached to every request you send.

  1. Go to platform.openai.com/api-keys
  2. Sign in (or create a free account)
  3. Click "Create new secret key"
  4. Copy the key — it looks like sk-... and you won't be able to see it again

OpenAI requires a small credit balance to use the API ($5 minimum). For casual scripts, $5 lasts months.


Step 4 — Write Your First AI Script

Open any text editor (Notepad on Windows, TextEdit on Mac, or VS Code if you have it). Create a new file called my_first_ai.py and paste this:

from openai import OpenAI

client = OpenAI(api_key="YOUR_API_KEY_HERE")

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "user", "content": "What are 3 simple ways to make money online with AI?"}
    ]
)

print(response.choices[0].message.content)

Replace YOUR_API_KEY_HERE with your actual API key from Step 3.

Save the file, then run it from your terminal:

python my_first_ai.py

You should see a response from ChatGPT printed directly in your terminal. That's it — you just wrote your first AI script.


What Just Happened

Here's what each part of that script does, in plain English:

  • from openai import OpenAI — loads the OpenAI library you installed
  • client = OpenAI(...) — creates a connection to OpenAI's servers using your key
  • client.chat.completions.create(...) — sends a message and waits for a reply
  • model="gpt-4o-mini" — specifies which AI model to use (gpt-4o-mini is fast and cheap)
  • messages=[...] — the conversation. You send a user message; the AI responds.
  • print(...) — outputs the AI's reply to your screen

The response.choices[0].message.content part just digs into the response object to extract the text. It looks weird at first but you'll type it from memory within a few days.


Making It Interactive

Right now the question is hardcoded. Let's make it so you can type anything:

from openai import OpenAI

client = OpenAI(api_key="YOUR_API_KEY_HERE")

question = input("Ask anything: ")

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "user", "content": question}
    ]
)

print("\nAI says:")
print(response.choices[0].message.content)

Run it and type any question when prompted. You've just built a command-line ChatGPT.


Free Option: Run It Locally with Ollama (No API Key, No Costs)

If you'd rather not use OpenAI's API or pay per request, you can run AI models completely free on your own computer. Our Llama 4 local install guide covers the full setup — Ollama makes it surprisingly easy.

Once Ollama is running, connecting Python to it is almost identical to the OpenAI script. Ollama's API is OpenAI-compatible, so you only change two lines:

from openai import OpenAI

# Point to your local Ollama instead of OpenAI's servers
client = OpenAI(
    base_url="http://localhost:11434/v1",
    api_key="ollama"  # any string works — Ollama doesn't require a real key
)

question = input("Ask anything: ")

response = client.chat.completions.create(
    model="llama4:scout",  # the model you have installed in Ollama
    messages=[
        {"role": "user", "content": question}
    ]
)

print("\nAI says:")
print(response.choices[0].message.content)

The result is the same — but everything runs on your machine for free. No API costs, no internet required after the initial model download.


Practical Ideas You Can Build Now

These are all beginner-friendly scripts you can build with what you just learned:

1. Article summarizer Paste a long piece of text and have AI summarize it in 3 bullet points. Useful if you read a lot of newsletters or research.

long_text = """Paste your article text here..."""

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{
        "role": "user",
        "content": f"Summarize this in 3 bullet points:\n\n{long_text}"
    }]
)
print(response.choices[0].message.content)

2. Email reply generator Paste an email you received and get a draft reply back. You review it, copy it, send it.

3. Product description writer Give it a product name and features, get 3 different marketing descriptions back. Useful for Etsy sellers, dropshippers, or anyone selling online.

4. Local AI chatbot Combine the Ollama version above with a simple loop to create a back-and-forth conversation that saves to a text file — your own private, offline AI assistant.


Organizing Your Scripts

Right now you have one script in one folder. As you build more, you'll want to stay organized.

A simple structure that works:

my-ai-scripts/
├── summarizer.py
├── email_helper.py
├── chatbot.py
└── .env           ← store your API key here, not inside scripts

Storing API keys safely: Instead of hardcoding your key inside scripts, use a .env file:

  1. Install python-dotenv:
pip install python-dotenv
  1. Create a .env file (no extension, just the name .env):
OPENAI_API_KEY=sk-your-key-here
  1. Load it in your script:
from dotenv import load_dotenv
import os

load_dotenv()

client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

This way, your API key never ends up in a script you accidentally share online.


Running Bigger Models in the Cloud (No Expensive GPU Needed)

If you want to experiment with larger open-source models — Llama 4 Maverick, Qwen 3, or Mistral — but your laptop isn't powerful enough to run them locally, Ampere Cloud gives you access to high-end GPUs by the hour. You SSH in, run your Python scripts on their hardware, and only pay while you're using it. Much cheaper than buying a dedicated GPU for experiments.


FAQ

Q: Do I need to know Python well to use AI tools?
A: No. You need to know enough to run a script and change a few values. The scripts in this guide cover 90% of what beginner AI builders actually do. You learn the rest by building.

Q: Is the OpenAI API free?
A: There's no free tier for API access, but $5 in credits lasts a very long time for personal scripts. At gpt-4o-mini pricing, $5 covers roughly 5–10 million tokens — enough for thousands of requests.

Q: What's the difference between using ChatGPT's website vs. the API?
A: The website is interactive — you type, it responds, you read. The API lets your scripts talk to the same models programmatically. You can automate tasks, process batches of data, and build tools that chain AI responses together.

Q: Can I use this on a Chromebook or low-end laptop?
A: Yes — Python + the OpenAI API runs fine on any computer. The AI processing happens on OpenAI's servers, not yours. For local models, you do need more hardware — but for API-based scripts, any machine from the last 10 years works.

Q: What should I learn next after this?
A: Try building one of the practical examples above. Then look into reading files with Python (open() and read()) — it lets your scripts process documents, spreadsheets, or websites before sending them to AI. That unlocks a huge range of automation projects.

Q: I got an error: ModuleNotFoundError: No module named 'openai'
A: This means pip install openai didn't install to the same Python your script is using. Try pip3 install openai or run the script with python3 my_first_ai.py instead of python.


You're now set up to build with AI in Python. The gap between "interesting AI demo" and "tool I actually use" is usually just one small script. Start with the email helper or summarizer — something that solves a real problem for you personally — and the rest will follow naturally.

Alex the Engineer

Alex the Engineer

Founder & AI Architect

Senior software engineer turned AI Agency owner. I build massive, scalable AI workflows and share the exact blueprints, financial models, and code I use to generate automated revenue in 2026.

Related Articles