Watch on Instagram

Code Bytes · "Intro to Programming" · Episode 2 · What code is

Compiled vs interpreted

A computer's processor only runs machine code, so your code has to be translated. A compiler translates the whole program before it runs; an interpreter, like the one Python usually uses, reads and runs it line by line, so a typo on line two stops the program after line one has already run.

Posted · About a 1 minute watch, 3 minute read · Take the quiz

Why code needs translating

A computer's processor only understands machine code: simple instructions stored as ones and zeros. The code you write, like print("Hi"), is made for people to read, so something has to translate it before the processor can run it. There are two main ways to do that.

Compilers: the whole thing first

A compiler translates the whole program before any of it runs, like translating a whole book before anyone reads it. The result is a new file of machine code, and only then does the processor start running it.

`` whole program → compiler → machine-code file → run ``

Because the compiler reads everything first, it usually rejects a mistake such as an unknown function name before the program ever starts.

Interpreters: line by line

An interpreter reads your program and runs it as it goes, like a live translator at a meeting. Python usually works this way. You can see it with a typo:

``python print("Line one runs") prnt("Line two") print("Line three") ``

Python runs line one, then reaches line two and stops, because prnt (print with the i missing) is not a name it knows:

`` Line one runs NameError: name 'prnt' is not defined. Did you mean: 'print'? ``

(Above that last line Python also prints a short "Traceback" that says which line failed.) Line three never runs.

Which is better?

Neither, they suit different jobs. Compiled code often runs faster, because the translating is all done ahead of time. Interpreted code is quick to try out and change: edit a line, run it again, see what happens. That is one reason Python is so popular for learning.

Try it yourself

Open any free online Python editor (search "online Python") and run the three-line program above. You should see Line one runs and then the error. Now fix the typo (change prnt to print) and run it again:

`` Line one runs Line two Line three ``

All three lines run once Python can understand every line.

Good to know

  • Every snippet on this page was run with Python 3 (3.13).
  • "Python usually uses an interpreter" is the standard beginner description of the main Python (CPython). Strictly, CPython first compiles the whole file into bytecode, and then its interpreter (a virtual machine) runs that bytecode instruction by instruction. Some other versions of Python compile further.
  • "Compiled" and "interpreted" describe how a language is usually run, not the language itself, and many languages mix the two, for example by compiling to bytecode and then interpreting or just-in-time compiling it.
  • "Only understands machine code" means the processor runs machine instructions, which are stored as binary.
  • The typo demo works because prnt is a name Python only checks when it reaches that line (a NameError while running). A grammar mistake, such as a missing bracket, is a SyntaxError: Python finds it while reading the whole file, so then nothing prints at all.
  • A compiled language would usually reject an unknown function name before the program ever runs.
  • "Often faster" is a general tendency, not a rule.

Answer to the comment question

Before the error, what does this program print?

``python print("A") print("B") pront("C") ``

A, then B. Python runs the program line by line: line one prints A, line two prints B, and on line three it stops with NameError: name 'pront' is not defined. Did you mean: 'print'?, so C is never printed.

Test your knowledge

Quick quiz: Compiled vs interpreted

4 multiple-choice questions. Pick one answer for each, then check how you did.

1What is the only kind of instruction a computer's processor runs directly?
2What does a compiler do?
3Python runs print("Line one runs"), then prnt("Line two"), then print("Line three"). What happens?
4Which is usually true?

Sources

Video transcript

Why does some code get translated first, and some get read line by line?

A computer's processor only understands machine code: simple instructions in ones and zeros. So your code needs translating.

Some languages use a compiler. It translates the whole program first, like translating a whole book before anyone reads it.

Python usually uses an interpreter. It reads your program and runs it line by line, like a live translator at a meeting.

Watch. Line one prints. Line two has a typo: print with the i missing. Python stops there with an error, and line three never runs.

Compiled code often runs faster. Interpreted code is quick to try out and change.

Before the error, what does this program print? Comment your answer below!

That's your byte.

More from "Intro to Programming"

All "Intro to Programming" lessons