Python  

Is Python a Compiled Language or an Interpreted Language?

🐍 Introduction

A common question that beginners often ask is: "Is Python a compiled language or an interpreted language?" The answer is not a simple one. Python does not fall completely into just one category. Instead, it uses a mix of both. To really understand this, we need to look at how Python code is executed step by step.

⚙️ How Python Code is Executed

When you write Python code in a file (for example, program.py) and run it, two main steps take place.

Step 1: Compilation into Bytecode 📝

  • Python does not directly run the code you write.

  • First, Python translates your code into something called bytecode.

  • Bytecode is a lower-level form of your code that is easier for the computer to understand but still not the machine code your CPU runs.

  • This compiled bytecode is often saved in .pyc files inside a folder named __pycache__.

For example:

# hello.py
print("Hello, CSharp Corner")

When you run this file, Python will first convert it into bytecode.

Step 2: Execution by the Interpreter 🖥️

  • Once Python has the bytecode, it passes it to the Python Virtual Machine (PVM).

  • The PVM is part of the Python interpreter. It reads the bytecode and executes it line by line.

  • This is why Python is often called an interpreted language.

🔄 Compiled vs Interpreted Languages

To better understand Python, let’s first recall the difference:

Compiled Languages (like C, C++)

  • The code is directly converted into machine code that the CPU can run.

  • Programs run very fast once compiled.

  • You must compile the program before running it.

Interpreted Languages (like JavaScript, Ruby)

  • The code is read and executed line by line by an interpreter.

  • They are usually slower compared to compiled languages.

  • Easier for developers to test and debug since they don’t need a full compile step.

Python combines both approaches because it first compiles into bytecode and then interprets that bytecode.

🏗️ Different Python Implementations

There are different versions (implementations) of Python, and they may execute code differently.

  • CPython (default)

    • This is the version of Python that most people use.

    • It compiles Python code into bytecode and then interprets it using the PVM.

  • PyPy

    • This version uses something called Just-In-Time (JIT) compilation.

    • It speeds up Python by converting bytecode into machine code while the program is running.

  • Jython

    • Runs Python code on the Java Virtual Machine (JVM).

    • Compiles Python code into Java bytecode.

  • IronPython

    • Runs Python code on the .NET framework.

    • Compiles Python code into .NET bytecode.

This shows that Python’s behavior depends on which version (implementation) you are using.

🧩 Example: Viewing Python Bytecode

We can actually see the bytecode that Python generates using the dis module:

import dis

def greet():
    print("Hello, CSharp Corner")

dis.dis(greet)

Sample output:

  2           0 LOAD_GLOBAL              0 (print)
              2 LOAD_CONST               1 ('Hello, CSharp Corner')
              4 CALL_FUNCTION            1
              6 RETURN_VALUE

This proves that Python code is not directly interpreted line by line from the source. Instead, it is first compiled into bytecode.

📌 Summary

Python is neither purely compiled nor purely interpreted. Instead, it follows a two-step process. First, Python code is compiled into bytecode, and then that bytecode is interpreted by the Python Virtual Machine (PVM). The standard version, CPython, uses this approach, while other implementations like PyPy or Jython may work differently. In simple words: Python is an interpreted language with a compilation step in between.