🐍 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 📝

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 🖥️

🔄 Compiled vs Interpreted Languages

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

Compiled Languages (like C, C++)

Interpreted Languages (like JavaScript, Ruby)

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.

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.