DEV Community

Cover image for Understanding Python: Interpreted vs. Compiled with a Practical Example
Anushka Kishor
Anushka Kishor

Posted on

Understanding Python: Interpreted vs. Compiled with a Practical Example

When learning about programming languages, one of the fundamental concepts to grasp is the difference between interpreted and compiled languages. Let’s take a practical journey with a Python file, from creation to execution, to understand why Python is classified as an interpreted language despite involving some compilation steps.

Step 1: Writing the Python Script

Imagine you write a simple Python script named hello.py:

This is your source code written in Python, a high-level programming language.

Step 2: Running the Python Script

When you run this script using the command:

Here’s what happens behind the scenes:

1. Compilation to Bytecode:

Hidden Compilation: The Python interpreter first translates your source code into an intermediate form known as bytecode. This step is automatic and hidden from you, the programmer. You don’t need to run a separate command for this; it happens when you execute your script.

Bytecode File: For efficiency, Python might store the compiled bytecode in a .pyc file within the pycache directory. For instance, running hello.py might create a file like pycache/hello.cpython-312.pyc (the name can vary depending on your Python version). This file is a compiled version of your script but in a form that is still not machine code.

2. Interpreting Bytecode:

Execution: The Python virtual machine (PVM) reads and executes the bytecode. This means the PVM interprets the bytecode instructions line by line, converting them into machine code on the fly.

Dynamic Execution: Python can execute scripts interactively, allowing you to run and test code snippets immediately. This feature is common in interpreted languages.

Compiled Languages vs. Interpreted Languages

To better understand the distinction, let’s contrast this with how a compiled language like C works:

1. C Language Workflow:

Source Code: You write C code in a file, say hello.c.

Compilation: You run a compiler (e.g., gcc hello.c) which converts the source code into machine code (a binary executable). This step is explicit and separate from running the code.

Execution: You run the resulting executable file (e.g., ./a.out), which directly executes on the machine’s hardware.

2. Key Differences:

Visibility of Compilation: In compiled languages, the compilation step is explicit and produces a separate executable file. In Python, the compilation to bytecode is implicit and handled by the interpreter.

Execution: Compiled code runs directly on the hardware, offering potential performance benefits. Interpreted code runs within an interpreter, adding a layer between your code and the hardware.

Why Python is Called an Interpreted Language

Despite the compilation step to bytecode, Python is termed an interpreted language for several reasons:

1. Implicit Compilation: The bytecode compilation is an internal process that happens automatically. As a programmer, you interact with Python as an interpreted language, writing and executing code directly without a separate compilation step.

2. Interactivity: Python supports interactive execution, allowing you to enter and run code in a REPL (Read-Eval-Print Loop) environment. This interactivity is a hallmark of interpreted languages.

3. Execution by PVM: The final execution of Python code is done by the Python virtual machine, which interprets the bytecode instructions. This layer of interpretation is what fundamentally defines Python’s behavior.

Conclusion

In summary, Python combines elements of both interpreted and compiled languages but is predominantly considered an interpreted language. The journey from writing hello.py to seeing "Hello, World!" on your screen involves an unseen compilation to bytecode followed by interpretation by the Python virtual machine. This unique blend allows Python to be both powerful and user-friendly, making it a favorite among developers for scripting, web development, data analysis, and more.

Top comments (0)