IS PYTHON INTERPRETED LANGUAGE OR COMPILED LANGUAGE ??




There is a lot of debate across the world  about whether Python is an interpreted language or a compiled language so in this blog let's clear out the confusion

For this let us understand what is meant by the terms “interpreted” and “compiled”

INTERPRETED LANGUAGE:-

Interpreted languages are executed line by line by an interpreter, which translates high-level code into machine code during run time. This means that the source code is directly executed without a prior compilation step.

Example:- Javascript, Ruby, R, PHP, Perl

Let’s understand with Python code



When you run this Python script the following steps are executed behind the scenes:

1. Source Code: You write the script in a file called example.py.

2. Execution: You run the script using the Python interpreter by typing python example.py in the terminal.

3. Interpretation: The Python interpreter reads the file, compiles it to bytecode, and then executes the bytecode line by line.

4.    Output: The output Hello, World! is printed to the terminal.

 

COMPILED LANGUAGE:-

Compiled languages are those in which the source code is translated into machine code by a compiler before execution. This machine code is then executed directly by the computer's hardware

 Examples:- C++, C, Rust, Go, Fortran , Ada

Let’s understand with Python code



When you compile and run this program following steps are executed behind the scenes:

 

1.    Source Code: You write the program in a file called example.c.

2.    Compilation: You compile the program using a C compiler (e.g., gcc example.c -o example). This process translates the C source code into machine code, producing an executable file (e.g., example).

3.    Execution: You run the executable file directly on the operating system (e.g., ./example).

4.    Output: The output Hello, World! is printed to the console.

 

 

Now that we understand what Complied and Interpreted Languages mean let’s go to python

Python is generally classified as an interpreted language, but its inner workings use a hybrid approach involving a combination of both interpretation and compilation.

Let’s understand it in a deeper

1. When you write Python code in a .py file. This code is human-readable and written in Python's high-level syntax.

2. When you run a Python program, the Python interpreter first compiles the source code into bytecode.

Bytecode is a low-level, platform-independent representation of your source code. Which is usually hidden in online compilers but we are able to see it in core Python when you execute in vs code editor or some other editors

 3. This compiled bytecode is stored in .pyc files.

But what is pcy. A .pyc file is a compiled Python file. When you run a Python script, the Python interpreter compiles the source code (written in a .py file) into bytecode, which is a lower-level, platform-independent representation of your code. This bytecode is then saved to a file with a .pyc extension, located in the __pycache__ directory by default.

4. The bytecode is then executed by the Python Virtual Machine (PVM). The PVM reads the bytecode and translates it into machine code that your computer's processor can understand. This step-by-step execution of bytecode by the PVM gives Python its reputation as an interpreted language.

 

But Why Python is Considered Interpreted

Even though Python involves a compilation step (to bytecode), it is often called an interpreted language because:

  • The compilation to bytecode is automatic and happens behind the scenes.
  • The bytecode is executed by the PVM at runtime, and this execution model resembles that of traditional interpreted languages where code is parsed and executed line by line.

 

Conclusion

Python is compiled to bytecode and then interpreted by the PVM, combining elements of both compiled and interpreted languages.

Understanding this hybrid nature helps clarify why Python has the flexibility and ease of use commonly associated with interpreted languages, while still benefiting from some advantages of compilation, like portability and error checking before execution.

 

 

By the end of this blog, I hope that you clarify whether Python is a compiled or an interpreted language.

What are your thoughts on this ??

Comments

Popular posts from this blog

Which is better for solving repetitive tasks in Python: recursion or loops?

Which one is better IPv4 or IPv6