# How to learn to code

Read and trace short programs before you write your own. Then study worked examples, solve small exercises checked by tests, and come back to old topics every few days instead of starting a big project on day one.

By João Marcos Torres Gardingo. Updated November 16, 2026. https://klugg.app/en/guides/how-to-learn-to-code

## In short

- Read and trace short programs before you write your own. In a study across seven countries, many students could not predict what a short piece of code would output (Lister and colleagues, 2004).
- Worked examples with labeled steps raised quiz scores and cut dropouts and failures in a 265-student course, though exam scores did not clearly improve (Margulieux and colleagues, 2020).
- Write small functions against tests before you start a project, and solve a new exercise on an old topic every few days.
- Use AI for hints, not answers. In a math study with about 1,000 students, open GPT-4 access raised practice grades by 48% but left later exam scores 17% lower (Bastani and colleagues, 2025).

## Why does learning to code feel hard?

Learning to code feels hard because a beginner must hold three things in mind at once: the syntax, what each line does, and the plan for the whole program. A blank editor asks for all three together. Margulieux and colleagues note that dropout and failure rates in intro programming courses are regularly as high as 50% ([Margulieux et al., 2020](https://doi.org/10.1186/s40594-020-00222-7)).

Reading code is part of the gap. Lister and colleagues tested students from seven countries on two tasks: predict what a short piece of code outputs, and pick the right ending for a nearly complete program from a few options. They concluded ([Lister et al., 2004](https://doi.org/10.1145/1041624.1041673)):

> Many students were weak at these tasks, especially the latter task, suggesting that such students have a fragile grasp of skills that are a prerequisite for problem-solving.

The fix is to split the skill into smaller pieces and practice each one on purpose, starting with reading code before writing it. The steps below do that.

## Which programming language should you learn first?

The best first programming language is the one that fits your goal. For websites, start with JavaScript. For data, automation or general scripts, start with Python. Both are good first languages. Switching languages early costs more than any difference between them, so stay with one until you can write small programs without help.

Write your goal as something you want to be able to do. "Build a script that renames my photos by date" is better than "learn Python".

## How do you learn to code, step by step?

Learning to code works best when you move from reading code to writing it. Trace short programs first. Then study worked examples with labeled steps, rebuild code from shuffled lines, change working code, write small functions against tests and debug on purpose. Start a project once the basics feel familiar, and review old topics every few days.

1. **Read and trace before you write.** Take a short program and predict its output line by line. Then run it and compare. In a study of students from seven countries, many were weak at predicting what a short piece of code would output ([Lister et al., 2004](https://doi.org/10.1145/1041624.1041673)). Tracing is a skill you can train directly.
2. **Study a worked example with labeled steps.** Look at a full solution where each step has a name, such as "start a counter", "loop over the items", "update the counter". In a semester course with 265 students, these labeled examples improved quiz scores. They did not clearly improve exam scores, but fewer students dropped or failed the course ([Margulieux et al., 2020](https://doi.org/10.1186/s40594-020-00222-7)). Read more about [worked examples](https://klugg.app/en/guides/worked-examples).
3. **Rebuild it from shuffled lines.** A Parsons problem gives you the correct lines in random order. You put them in order and indent them. In one experiment, Parsons problems took less time than fixing or writing the same code, with no significant difference in learning one week later ([Ericson et al., 2017](https://doi.org/10.1145/3141880.3141895)).
4. **Change working code.** Before you write from scratch, modify a working program. Add a condition, change the output, handle an empty list.
5. **Write small functions against tests.** Now write code from a short spec. Tests tell you right away if it works. When a test fails, find the reason before you change anything.
6. **Debug on purpose.** Take code with one bug. Find it, fix it and write one sentence on why it happened.
7. **Build a small project after the basics.** Projects combine many skills at once, which overloads a beginner. The balance shifts with skill: guidance that helps beginners becomes less useful, and can even get in the way, as you gain experience ([Kalyuga, 2007](https://doi.org/10.1007/s10648-007-9054-3)). Start the project once loops, functions and data structures feel familiar.
8. **Review old topics.** Every few days, solve a new exercise on a topic from last week. Answering from memory and spacing the reviews both help you keep what you learned ([Yang et al., 2021](https://doi.org/10.1037/bul0000309); [Cepeda et al., 2006](https://doi.org/10.1037/0033-2909.132.3.354)). See [retrieval practice](https://klugg.app/en/guides/retrieval-practice) and [spaced repetition](https://klugg.app/en/guides/spaced-repetition).

## Should you use AI while learning to code?

Use AI while learning to code as a tutor, not as the author. AI help makes coding tasks easier, but easier practice does not always mean more learning. Try each exercise first, ask for a hint instead of the answer, and measure your progress with the AI turned off. The two studies below show why.

In a field experiment with about 1,000 high school students, open access to GPT-4 raised grades on practice problems by 48%. When the access was removed, those students scored 17% lower on the exam than students who never had it. A version built as a tutor, with safeguards, removed most of that harm ([Bastani et al., 2025](https://doi.org/10.1073/pnas.2422633122)). That study was in math, not programming.

In programming, a study with 69 novices aged 10 to 17 found that an AI code generator raised task scores and did not hurt manual code changes. The gain on a test one week later was small and not significant ([Kazemitabaar et al., 2023](https://doi.org/10.1145/3544548.3580919)).

More on this in [AI tutoring](https://klugg.app/en/guides/does-studying-with-ai-work).

## What does a week of learning to code look like?

A beginner's week of learning to code, at about one hour a day, mixes new ideas, practice and review. Two days bring a new concept, two days bring exercises, one day goes to review and debugging, and Saturday holds mixed practice or project time. Sunday is for rest or a short written note on what you learned.

| Day | Activity | Time |
| --- | --- | --- |
| Monday | New concept: trace 3 short programs, then study one worked example | 1 h |
| Tuesday | Two Parsons problems, then modify the worked example | 1 h |
| Wednesday | Two small functions against tests | 1 h |
| Thursday | Review: one new exercise on last week's topic, one debugging task | 45 min |
| Friday | Second new concept: trace, then a worked example | 1 h |
| Saturday | Mixed exercises from both concepts, or project time once the basics are in place | 1 h 30 |
| Sunday | Rest, or explain one thing you learned in a short written note | 15 min |

To build your own, see [how to make a study plan](https://klugg.app/en/guides/how-to-make-a-study-plan).

## What mistakes slow down new programmers?

The mistakes that slow down new programmers feel like progress but skip the hard part: writing code alone. Beginners often watch tutorials without typing, start with a big project, let an AI write the code, rerun a failed test without finding the cause, and practice one topic in a long block without coming back to it.

- **Watching tutorials and never typing.** Following along feels like progress. Writing code alone is the real test.
- **Starting with a big project.** It mixes too many new things. You get stuck and blame yourself.
- **Letting the AI write the code.** The exercise gets done. You do not learn the skill.
- **Reading a failed test as "try again".** Find which idea is wrong before you change code.
- **Practicing one topic in a long block and never coming back.** Mix old topics into new sessions. See [interleaving](https://klugg.app/en/guides/interleaving).

## How Klugg does this

Klugg is an AI study tutor that turns a programming goal into a plan, cited lessons, code exercises and spaced reviews. You study in the browser, on a computer or a phone. The hosted beta is invite-only, with a waitlist.

- You describe a goal, such as "write Python scripts to clean spreadsheets". Klugg builds a path of topics and starts it. When research finds sources, Klugg updates the path and says what changed, with Undo.
- A short placement check marks the topics you already know, and their lessons skip the worked steps.
- For programming goals, Klugg uses a reviewed study playbook. The "How you'll learn this" panel shows each step and the studies behind it.
- Each lesson gives the idea, a worked example and the most common mistake. Sentences link to the sources they rely on, and Klugg removes a citation that does not match a passage in the source.
- In code exercises, you write a JavaScript or Python function in a code editor in your browser. Tests run there and check it, with no AI grading.
- Stuck? Ask for a hint first, then a worked step, then the answer. Klugg records the help you asked for.
- A code topic comes back in review as a new exercise, on a date set by the FSRS algorithm. Each topic climbs a mastery ladder from "Seen" to "Recalled later", and reading or skipping a topic never counts as mastery.

## Questions

### What programming language should I learn first?

Pick by goal. JavaScript for websites, Python for data, automation and general scripts. Both work well as a first language. Sticking with one matters more than which one.

### How long does it take to learn to code?

There is no research-backed number. It depends on your goal and your hours. Write small working programs from the first week and measure progress by what you can build without help.

### Can I learn to code with ChatGPT or another AI?

Yes, if you use it as a tutor and not as the author. Try each exercise first, ask for hints, and check yourself without the AI. Studies show open AI help can raise practice scores while lowering what you can do alone.

### Should I build projects or do exercises?

Exercises first, projects soon after. Small exercises with tests build each skill. A project makes sense once loops, functions and basic data structures feel familiar.

## Sources

1. Lister, R., Adams, E. S., Fitzgerald, S., et al. (2004). A multi-national study of reading and tracing skills in novice programmers. ACM SIGCSE Bulletin, 36(4), 119-150. https://doi.org/10.1145/1041624.1041673
2. Margulieux, L. E., Morrison, B. B., & Decker, A. (2020). Reducing withdrawal and failure rates in introductory programming with subgoal labeled worked examples. International Journal of STEM Education, 7, 19. https://doi.org/10.1186/s40594-020-00222-7
3. Ericson, B. J., Margulieux, L. E., & Rick, J. (2017). Solving Parsons problems versus fixing and writing code. Koli Calling International Conference on Computing Education Research. https://doi.org/10.1145/3141880.3141895
4. Kalyuga, S. (2007). Expertise reversal effect and its implications for learner-tailored instruction. Educational Psychology Review, 19(4), 509-539. https://doi.org/10.1007/s10648-007-9054-3
5. Kazemitabaar, M., Chow, J., To, C. K., Ericson, B. J., Weintrop, D., & Grossman, T. (2023). Studying the effect of AI code generators on supporting novice learners in introductory programming. CHI Conference on Human Factors in Computing Systems. https://doi.org/10.1145/3544548.3580919
6. Bastani, H., Bastani, O., Sungu, A., Ge, H., Kabakcı, Ö., & Mariman, R. (2025). Generative AI without guardrails can harm learning: Evidence from high school mathematics. Proceedings of the National Academy of Sciences, 122(26). https://doi.org/10.1073/pnas.2422633122
7. Yang, C., Luo, L., Vadillo, M. A., Yu, R., & Shanks, D. R. (2021). Testing (quizzing) boosts classroom learning: A systematic and meta-analytic review. Psychological Bulletin, 147(4), 399-435. https://doi.org/10.1037/bul0000309
8. Cepeda, N. J., Pashler, H., Vul, E., Wixted, J. T., & Rohrer, D. (2006). Distributed practice in verbal recall tasks: A review and quantitative synthesis. Psychological Bulletin, 132(3), 354-380. https://doi.org/10.1037/0033-2909.132.3.354
