Lesson 06
Interpreted Programming
How Python compiles to byte code and executes via the PVM.
What you will learn
By the end of this lesson, you'll understand:
- What happens when you run a
.pyfile? - 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.
The user only runs:
python program.pyInternally, Python handles everything else.
Process 1 · Compilation
The first stage is Compilation.
What the compiler does
- Reads your source code
- Checks for syntax errors
- Converts valid code into Byte Code
- Reports syntax errors if they exist
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.
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.
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.
print("Statement-1")
print("Statement-2")
print(10 / 0)
print("Statement-4")What happens?
Output
Statement-1
Statement-2
ZeroDivisionError: division by zeroNotice 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.
print("Statement-1")
try:
print(10 / 0)
except ZeroDivisionError:
print("Cannot divide by zero")
print("Statement-3")Output:
Statement-1
Cannot divide by zero
Statement-3Difference
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.
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.
print("First")
print("Second")
try:
print(10 / 0)
except ZeroDivisionError:
print("Division not possible")
print("Fourth")
print("Fifth")Expected output:
First
Second
Division not possible
Fourth
FifthKey Points
- Python source files use the
.pyextension. - 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
- What is the extension of a Python source file?
- What is byte code?
- What is the role of the Python Virtual Machine (PVM)?
- Why is Python called an interpreted language?
- 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.