Society and work

How do you use AI to learn to code from scratch?

· By Robert Scales · 3 min read

Short answer

Use the AI as a tutor, not as a code vending machine: ask it to explain ideas, give hints and review code you wrote yourself, and tell it not to hand you finished solutions. You learn by typing, running and fixing the code; the AI's job is to keep you unstuck.

A chatbot such as ChatGPT can write a working Python program in seconds, which is exactly why it can get in the way of learning. If it writes every exercise for you, you finish the course without the skill. The same tool, set up differently, becomes something beginners rarely had before: a tutor who answers the fifth "why?" in a row without sighing, at any hour.

The difference lies in the instructions you give it. Ask for explanations, questions and hints, and keep the keyboard for yourself. Programming is learned in the hands: writing a line, running it, reading the error, and fixing it.

What a good AI tutor does

Harvard's introductory computer science course, CS50, offers a well-documented example. In 2023 its staff built their own AI helpers for students, first for about 70 summer students and then for thousands online and roughly 500 on campus. The course limited general tools like ChatGPT and GitHub Copilot and steered students toward its own. The centerpiece is the CS50 Duck, named after rubber-duck debugging, the old habit of explaining your code out loud to a toy duck until you spot the bug yourself.

The duck runs on GPT-4, but with what the team calls pedagogical guardrails: rules in its system prompt (the hidden instructions that set its behavior) that make it guide students toward an answer instead of giving it. A second tool explains highlighted lines of code in plain English. Students reported that it felt like having a personal tutor.

You can't install CS50's guardrails in your own chat, but you can write similar rules into your first message. Khan Academy's Code Tutor, one of the custom GPTs featured when OpenAI opened its GPT Store in January, is built around the same idea, if you have a paid ChatGPT plan.

A worked tutor prompt: explain before you write

Paste this at the start of a new conversation and adjust the bracketed parts.

Prompt
You are my programming tutor. I am a complete beginner learning [Python] so I can [automate small tasks at work].

Rules for this whole conversation:
1. Never write a full solution to an exercise unless I type "SHOW SOLUTION".
2. When I ask how to do something, first explain the idea in plain words with a tiny example that is NOT my exercise.
3. Then ask me to try. If I'm stuck, give one hint at a time, smallest hint first.
4. When I paste my code, tell me what works, then point to ONE problem and ask a question that helps me find the fix.
5. When I paste an error message, explain what it means in plain words before suggesting where to look.
6. End each explanation by asking me to explain it back in my own words.

My first exercise: write a program that asks for a name and prints a greeting.

Rule 2 matters most. An example that differs from your exercise forces you to transfer the idea, which is where learning happens. Rule 6 turns the chat into a check of your understanding rather than a stream of text you skim.

A practice plan for the first stages

StageWhat you practiceHow the AI helps
1. BasicsVariables, input and output, simple mathExplains each term; quizzes you with three short questions
2. Decisions and loopsif, for, whileGives hints when a loop never stops; asks you to predict output
3. Functions and listsBreaking a problem into piecesReviews your code for readability, one comment at a time
4. A small projectA to-do list, a quiz game, a file renamerHelps you plan steps in plain words before any code

Keep one habit through every stage: run everything yourself, in a free online editor or on your own computer. The AI can be wrong, and running the code is how you find out.

Try it: Start a chat with the tutor prompt above. Solve the greeting exercise without typing "SHOW SOLUTION", then ask the tutor for a slightly harder variation and solve that too.

Why this beats copying from the chat

A tutor-style setup makes you do the three things that build skill: recall (remembering how a loop works), struggle (trying before seeing an answer) and feedback (hearing exactly what is wrong). Copying skips all three. It also hides a risk: AI-written code can contain mistakes that look plausible, and a beginner who didn't write it can't tell.

How do you avoid copying code you don't understand?

Use a simple rule: no line goes into your program until you can say what it does. If the AI shows you code, for example after you type "SHOW SOLUTION", go through it line by line and write a short comment above each line in your own words. Where you can't, ask the AI about that line only.

Then test your understanding instead of trusting the feeling of understanding. Before running the code, predict what it will print. Change one thing, such as the loop's range or a condition, and predict again. If your predictions keep matching, you understand it. If they don't, you have found exactly what to ask your tutor next.

Finally, retype instead of pasting. It feels slower, but typing makes you notice every symbol, and the next time you need that pattern, you will be able to write it from memory.

Sources

  1. Teaching CS50 with AI: Leveraging Generative Artificial Intelligence in Computer Science Education — Harvard University (Liu, Zenke, Liu, Holmes, Thornton, Malan), SIGCSE 2024, March 20, 2024
  2. Introducing the GPT Store — OpenAI, January 10, 2024

Get the free Guide to Artificial Intelligence

Sign up for the weekly newsletter and receive the free guide: what changed in AI this week, one lesson, one prompt to try. Unsubscribe in one click.

How we handle your address

Back to top

Display

Your choices are saved in this browser only. No data is sent.

Text size
100%
Colours
Reading
More settings
Accessibility statement