What I have been taught in school

During my first year of university, I had heard of interpreted and compiled languages. I wondered, "What are they?"
The answer can be explained with a well-known analogy:

  • A compiled language is like having a translator translate an entire document into another language.
  • An interpreted language is similar to an interpreter

In this article, I take C for compiled language and Python for interpreted one as they are popular.
So in my imagination:

  • C code is compiled all at once into an execute file. This file is executed by CPU.
  • Python code is interpreted, so CPU reads python file and executes the program line by line. However, this explanation is too high-level.

My confusion when I learn more

  1. In computer system class, I learned that The CPU fetches instructions from memory and executes them one by one. This is the same for every instructions, for every programming languages. But this sounds very similar to what an interpreter does, doesn't it? Does this conflict or even relate with the compiled/interpreted knowledge above?
  2. If it doesn't conflict. Then compiled or interpreted language is not decided by how CPU executes code but by another factor. Let's breakdown how C and Python are converted into instructions for CPU

How C works

The following diagram illustrates how a source file is processed to generate an executable file. The executable file is already in machine-readable format so CPU can execute it directly.
Compilation process in C

How Python works

Python has multiple implementations. We have CPython (implemented by C), Jython (implemented by Java),... We use CPython in this article.
Python has both a compiler and an interpreter. The following diagram illustrates how a Python file is processed:

How Python executes a script

So in Python, .py file is still compiled into a bytecode file. Python VM (implemented in C) reads this file, converts bytecode to machine instructions. These instructions are fetched by CPU to execute.
If Python has both compiler and interpreter, why do we call it an interpreted language? This is because the compiled bytecode is interpreted at runtime rather than being entirely translated into native machine code ahead of time.

Confusion solved

Regarding the first confusion:

  • Compilation and interpretation describe how a program is translated and executed, not whether the CPU executes instructions sequentially. So there is no conflict in the first confusion. Regarding the second confusion:
  • Compilation: the whole program is translated into another representation before that representation is executed.
  • Interpretation: Software (VM or interpreter) processes a program's instructions at runtime and implements their behavior.