Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 06

Interpreted Programming

How Python compiles to byte code and executes via the PVM.

Beginner10 min

What you will learn

By the end of this lesson, you'll understand:

  • What happens when you run a .py file?
  • What are the two internal processes: Compilation and Execution?
  • What is the role of the Python Virtual Machine (PVM)?
  • Why is Python called an interpreted programming language?
  • How do runtime errors affect program execution?

What happens when you run a Python file?

When you save a program as program.py and execute it, Python performs two internal processes.

.py source to compile to PVM to output

The user only runs:

bash
python program.py

Internally, Python handles everything else.

Process 1 · Compilation

The first stage is Compilation.

What the compiler does

  1. Reads your source code
  2. Checks for syntax errors
  3. Converts valid code into Byte Code
  4. Reports syntax errors if they exist
Read, check syntax, generate byte code, store

Byte Code

Byte code is an intermediate representation of your program.

It is not machine code and not your original source code.

Think of it as a bridge between Python and the operating system.

Process 2 · Execution

After compilation, the Python Virtual Machine (PVM) begins execution.

Definition

PVM (Python Virtual Machine) is the component of Python that reads byte code line by line and converts it into executable machine instructions.

Byte code through PVM to machine execution

The PVM executes your program during runtime.

Why is Python called an Interpreted Language?

Python is called interpretation-based because execution happens through the interpreter pipeline.

Write, compile, PVM executes, output

Unlike many traditional compiled languages, you typically do not create a permanent .exe for every script.

This makes Python excellent for:

  • Learning
  • Automation
  • Rapid prototyping
  • Debugging
  • Testing

Runtime Error Example

Consider this program.

python
print("Statement-1")
print("Statement-2")
print(10 / 0)
print("Statement-4")

What happens?

Statements run until division by zero stops later code

Output

text
Statement-1
Statement-2
ZeroDivisionError: division by zero

Notice that Statement-4 never executes because the runtime error stops the program.

Handling Runtime Errors

Instead of allowing the program to stop, use try / except.

python
print("Statement-1")

try:
    print(10 / 0)
except ZeroDivisionError:
    print("Cannot divide by zero")

print("Statement-3")

Output:

text
Statement-1
Cannot divide by zero
Statement-3

Difference

Without try With try
Program stops Program continues
Error shown Friendly message
Later code skipped Later code executes

This is one reason Python is considered robust.

Why Developers Love the Interpreter

Strength Benefit
Rapid Development Edit → Save → Run immediately
Easy Debugging Errors appear during execution
Automation Perfect for scripts and DevOps
Experimentation Ideal for AI and data science

Python's interpreter encourages a fast edit–run–debug workflow.

Practice Exercise 1

Create five print statements.

python
print("First")
print("Second")
print(10 / 0)
print("Fourth")
print("Fifth")

Question: Which messages appear?

Answer: Only First and Second appear before the runtime error.

Practice Exercise 2

Modify the program using exception handling.

python
print("First")
print("Second")

try:
    print(10 / 0)
except ZeroDivisionError:
    print("Division not possible")

print("Fourth")
print("Fifth")

Expected output:

text
First
Second
Division not possible
Fourth
Fifth

Key Points

  • Python source files use the .py extension.
  • The compiler converts source code into byte code.
  • The Python Virtual Machine (PVM) executes byte code line by line.
  • Runtime errors stop later statements unless handled.
  • The interpreter model enables fast development and debugging.

Interview Questions

  1. What is the extension of a Python source file?
  2. What is byte code?
  3. What is the role of the Python Virtual Machine (PVM)?
  4. Why is Python called an interpreted language?
  5. What happens to statements after a runtime error?

Summary

Step Description
.py Source code written by the programmer
Compilation Syntax checked and byte code generated
Byte Code Intermediate representation
PVM Executes byte code at runtime
Output Results displayed to the user

Python's interpreted execution model allows you to write, run, test, and debug programs quickly, making it ideal for scripting, automation, AI experimentation, and rapid software development.

Next

Next lesson: Platform Independent Programming — why the same Python program can run on Windows, macOS, and Linux with little or no modification.