Your First Python Prompts with ChatGPT, Claude & Gemini
You can write Python code by hand. You can also describe what you want in plain English and let an AI write it for you. The reality of how working data scientists code in 2026 is somewhere between the two: write a clear prompt, read the AI's response carefully, then test and modify the code.
This lesson teaches you how to ask AI for Python code in a way that gets useful answers the first time.
What You'll Learn
- A four-part prompt template that produces working code
- How to give AI the right context (data shape, libraries, constraints)
- How ChatGPT, Claude, and Gemini differ for Python work
- The "explain it back to me" technique that prevents subtle bugs
The Four-Part Code Prompt
Most beginner prompts fail because they leave out one of these four parts. Use them all every time.
1. Role and goal. "I am a beginner learning Python. I want to..." sets the level. AI tunes its explanation to your stated level.
2. Context. Describe your data shape, libraries you have, and any constraints. "I have a CSV with columns name, age, score. I am using pandas in Google Colab."
3. Task. What you want the code to do, in plain English. Be specific. "Compute the average score for everyone over 18, rounded to one decimal place."
4. Output format. What you want back. "Give me the code in one block, then explain each line in plain English."
Putting it together:
I am a beginner learning Python. I have a CSV with columns
name,age,score, and I am using pandas in Google Colab. Please write code that computes the average score for everyone over 18, rounded to one decimal place. Give me the code in one block, then explain each line in plain English.
That single prompt will give you something runnable, with the reasoning attached. Compare it with the lazy version: "how do I average scores in pandas?" The lazy version returns generic syntax; the structured version returns code you can paste into your notebook.
Real Example: Loading and Filtering a Dataset
Try this exact prompt in ChatGPT or Claude:
I am a beginner. I am in Google Colab. Please write Python that does the following:
- Loads the iris dataset from this URL:
https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv- Prints the first five rows
- Filters the data to only
species == 'setosa'- Computes the average
petal_lengthfor that filtered groupAfter the code, explain each step in one sentence.
You should get something like:
import pandas as pd
url = "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv"
df = pd.read_csv(url)
print(df.head())
setosa = df[df["species"] == "setosa"]
average_petal_length = setosa["petal_length"].mean()
print(f"Average petal length for setosa: {average_petal_length:.2f}")
Run it. If the answer looks reasonable (around 1.46), the AI succeeded. If it does not, that is a debugging exercise — which is the next lesson.
The "Explain It Back to Me" Technique
After the AI gives you code, ask it to explain in a different way. This catches misunderstandings before they bite.
Example follow-up:
Walk me through line 5 of that code. What does
df["species"] == "setosa"actually return on its own, before being put insidedf[...]? Show me the output.
The AI will reveal that df["species"] == "setosa" returns a boolean array — True for rows that match and False for rows that do not. Then df[boolean_array] keeps only the True rows. Now you understand boolean filtering in pandas, not just the recipe.
The pattern is: get code → run it → ask AI to explain the trickiest line in detail → ask "what would happen if I changed X?"
Differences Between ChatGPT, Claude, and Gemini
You can use any of the three for Python. The strengths differ:
- ChatGPT — strongest free general assistant. The paid tier ("Advanced Data Analysis") will actually run Python on a CSV you upload, which is great for verification.
- Claude — explains code carefully, holds long context, and tends to write cleaner, more idiomatic Python. Try the "Projects" feature: drop your data dictionary or course notes in once, and every chat in that project knows about them.
- Gemini — built into Google Colab itself. Open a Colab notebook and you will see a Gemini panel that can explain a cell, fix an error, or write a new cell from a description.
A practical combination: Gemini in Colab for inline help, Claude in a separate tab for the deeper "explain it back to me" conversations, and Perplexity when you need to look up library docs with citations.
What AI Will Get Wrong
AI confidently produces wrong code. The four most common failure modes for beginner Python prompts:
1. Outdated library APIs. The AI was trained on data through some cutoff date. Pandas, scikit-learn, and matplotlib all change function signatures. If you see a deprecation warning when you run the code, that is the AI being slightly out of date.
2. Hallucinated functions. The AI sometimes invents a function that does not exist (df.compute_average_pretty() is not a real method). If you get AttributeError: 'DataFrame' object has no attribute 'X', that is a hallucination.
3. Wrong data types. AI assumes column types. If your "score" column is actually strings, the average will fail. Always show the AI the first few rows and the column types when in doubt.
4. Silent assumptions. A pandas .mean() skips missing values by default. If you did not realize you had missing data, the average is computed over a smaller sample than you think. Always run df.info() and df.describe() to know what you are averaging over.
A Prompt Template You Can Save Forever
Save this as a note in Claude Projects, ChatGPT memory, or just a sticky note:
Role: You are an expert Python tutor for a complete beginner studying for AI and data science.
Context: I am using
[Colab / Jupyter / VS Code]. My data has these columns:[list]. The first three rows look like:[paste].Task:
[describe what you want the code to do].Constraints: Use only
[pandas / numpy / sklearn / standard library]. Keep the code under 30 lines. Add a comment on each line.Output: First the code in one block, then a plain-English explanation, then a tiny test I can run to confirm it worked.
Use it for the first month. After that, you will have internalized the structure and write good prompts naturally.
Key Takeaways
- A good code prompt has four parts: role, context, task, output format
- Always show the AI your data shape — column names, types, and a few rows
- After getting code, ask AI to explain the trickiest line and predict what changes would do
- AI gets things wrong; common failures include outdated APIs, hallucinated functions, and silent assumptions
- ChatGPT, Claude, and Gemini all work — pick one and learn it deeply before switching

