Techโ—โ—โ—โ—โ—Difficulty 2 of 5

Why does Python run your code as it goes while C translates it first?

Python doesn't really run line by line: it quietly translates your code into bytecode first, just not all the way to machine code.

โ–ถ Start the story

C is translated ahead of time because compiled code tends to run faster; Python runs through an interpreter because that makes code quicker to change and easier to carry between computers. Still, the two are less different than they look. A compiler translates your whole program into machine code ahead of time, and the processor then runs that. An interpreter is software that executes your code without first compiling it to machine code: it carries out the instructions itself as it goes. "Line by line" is a handy picture, but a simplification. In practice an interpreter may first translate your code into an intermediate format called . That is what CPython, the standard Python, does: it compiles your program to bytecode, which a then executes.

Why have both? Interpreting is slower, because the interpreter runs many machine instructions for every step of your program. A program compiled to tends to run faster, and setups land in the middle. But faster development can outweigh faster execution. With an interpreter the is short, which matters most when you are and testing. Python's developers typically prioritize readability over performance, and when speed really matters, hot spots can move into extension modules written in C.

Two ways to run a program

Compiled (like C)

  • Translated ahead of time to machine code
  • Tends to run faster
  • Need a build for each kind of computer

Interpreted (like Python)

  • Carried out by another program as it runs
  • Slower, but quick to edit and try
  • Runs anywhere there is an interpreter

Interpreters have another gift: portability. Code that runs in an interpreter can run on any that has a compatible interpreter, so you do not need to build an executable for each one. Most Python setups also offer a read-eval-print loop where you type one statement and get the answer immediately.

The idea has a charming origin. Lisp was the first interpreted high-level language. Steve Russell read John McCarthy's paper and realized, to McCarthy's own surprise, that the Lisp eval function could be written in machine code.

Quiz me

0/3

  1. 1.Why isn't "Python runs line by line" the whole story?
  2. 2.Why does interpreted code reach many kinds of computers with little extra work?
  3. 3.Why do programmers accept the slower speed of interpreting?

Recap

Compiling translates ahead of time for speed; interpreting runs code as it goes for flexibility, and many languages mix both.

๐Ÿ’ก A trick to remember it ยท A compiler is a printed translation; an interpreter is a live interpreter at your elbow.

Surprising fact ยท CPython does translate your code first, into bytecode that a virtual machine runs.

Sources (3)

No source, no claim. Every fact in this lesson (15 claims) cites at least one of these.

  1. [1]Interpreter (computing) ยท Wikipedia
  2. [2]Python (programming language) ยท Wikipedia
  3. [3]Compiler ยท Wikipedia
More lessons in ๐Ÿ’ป Tech (3) See all tech lessons โ†’

One more light on your map.

Get one lesson like this every day, about the things you love. Free, in two or five minutes.

Get the share card for this lesson โ†—