Is Python an interpreted or compiled language?
Python is generally considered an interpreted language, but it has characteristics of both interpreted and compiled languages. Here’s a breakdown:
Interpreted Nature:
When you run a Python script, the Python interpreter processes the code. Python code is executed line-by-line during runtime, which is a hallmark of interpreted languages. Compiled Aspect:
Before execution, Python code is compiled into bytecode, which is a lower-level, platform-independent representation of the source code. This bytecode is then executed by the Python Virtual Machine (PVM).
Key Points:
Compilation to Bytecode: This step is invisible to users but is why Python is sometimes said to be “partially compiled.”
No Standalone Executable: Unlike fully compiled languages (e.g., C or C++), Python does not produce standalone machine code directly. Instead, it relies on the interpreter to execute the bytecode.
So, while Python primarily functions as an interpreted language, its intermediate bytecode compilation adds a compiled element to its execution process.
Interview angle
- “Is Python interpreted or compiled?” - both. Source is compiled to bytecode (cached in
__pycache__), then the CPython VM interprets that bytecode. Calling it “just interpreted” misses the compilation step people ask about. - “Why is it slower than C?” - dynamic typing means every operation dispatches on runtime type, objects carry boxing overhead, and there is no ahead-of-time optimisation. Python 3.13+ ships an experimental JIT that narrows this for some workloads.
- “What is
.pycand when is it regenerated?” - cached bytecode, invalidated by source mtime or hash. It saves compilation time on import, not execution time.