All Courses
Advanced Python

Compilers & Interpreters

Key Definitions

Term Definition
Source code The code you write as a programmer (Python, Java, C++, etc.)
High-level language Code close to human English — easy for humans to read and write (e.g. Python)
Low-level language Code closer to hardware — harder for humans to read (e.g. bytecode, machine code)
Compiler A program that converts a high-level language into a lower-level language
Bytecode The lower-level language Python is compiled into before interpretation
Interpreter A program that reads bytecode and executes it line by line

How Python Code is Executed

Source Code (.py)
      ↓
  [ Compiler ]       ← performs syntax & formatting checks
      ↓
   Bytecode
      ↓
  [ Interpreter ]    ← executes bytecode line by line
      ↓
   Output / Result

Step 1 — Compilation

  • Your .py source code is fed to the compiler
  • The compiler performs checks:
  • Syntax errors (missing colons, incorrect indentation, etc.)
  • Formatting errors
  • If checks pass → source code is translated into bytecode
  • If checks fail → compilation error (the program never runs)

Step 2 — Interpretation

  • The bytecode is fed to the interpreter
  • The interpreter reads and executes the bytecode line by line
  • The interpreter converts bytecode into instructions the CPU can understand
  • If an error occurs here (e.g. dividing by zero) → runtime error

Compilation Errors vs Runtime Errors

Error Type When it occurs Example
Compilation error During the compile stage — before any code runs Missing colon, bad indentation
Runtime error During interpretation — while the program is running ZeroDivisionError, AttributeError

Compiler vs Interpreter — Summary

Compiler Interpreter
What it does Converts source code → bytecode Reads bytecode and executes it
When it runs Before the program starts While the program is running
Output Bytecode Program result / output
Errors caught Syntax & formatting errors Runtime errors

Python vs Compiled Languages (Java, C++)

  • Python is often called an interpreted language — most of the work happens at the interpretation stage
  • Java / C++ are called compiled languages — much more processing happens at the compilation stage (stronger type checking, more optimisations)
  • Both still go through compilation and interpretation — the distinction is where most of the work happens

Python Interpreters

Python has multiple interpreter implementations:

Interpreter Guides
CPython The default; also allows C code to run alongside Python
PyPy An alternative with performance optimisations
Others Various implementations for different use cases

Different interpreters are different implementations of the Python language — some offer efficiency gains depending on the use case.


Key Takeaways & Recap

Concept Summary
Compiler Converts high-level source code to lower-level bytecode; catches syntax errors
Interpreter Executes bytecode line by line; raises runtime errors
Python execution Source code → compiler → bytecode → interpreter → output
High-level language Close to English; what programmers write (Python, Java, etc.)
Low-level language Close to hardware; harder for humans to read (bytecode, machine code)
Python = "interpreted" Most work happens at interpretation; still compiled first