The Python Interpreter – Time to Upgrade your Programming Skills

We have learned that Python is an interpreted language.

Well, what does it mean?

It means that we actually run another program that is responsible for reading our source code and running it top to bottom.

In Python, this “another program” we just referred to is called an Interpreter. Simply put, an Interpreter is a kind of program that executes other programs.

Now, this interpreter itself can be implemented as a C program, or as a set of Java classes, or can be written in Python itself. The standard Python interpreter, though, is written in C, called the CPython.

How does Python Interpreter work?

In a nutshell, when you write a Python code, the interpreter reads the program and executes the instructions therein.

Let’s look at Python interpreter’s working through a programmer’s view, and then through the Python interpreter’s view itself..

The Programmer’s View

We start by creating a Python Program, which is just a text file containing Python statements and ending with a .py extension.

For example, one of the simplest yet fully function Python program is as follows:

print(‘Hello, World’)

Yeah, that’s it!

That one statement forms a complete Python program. It will simply print a string, that is, the text that’s written inside the quotes.

Code it yourself by opening up Python’s IDLE. For this, you’ll need to install Python’s latest version on your computer. This should open up Python’s shell.

Create a new file (CTRL + N) and write the code written above. Now save it by whatever name you wish, say hello.py.

Now let’s get the Python interpreter to execute our file.

This simply means that the Python interpreter will run all the statements in your file from top to bottom, one after another.

You can command the interpreter to execute your file by clicking on Run > Run Module at the top of your file, or simply by pressing the F5 key.

If everything goes right, you will see the text “Hello, World” printed out in blue in your Python Shell.

Output:

Hello, World
>>>

Easy, right?

Now let’s see what goes behind the execution of this simple one-line program.

The Interpreter’s view

The brief description in the prior section was usually all that a Python programmer needs to know.

Type code into the text file and then run that file through the Python Interpreter.

Under the hood, though, a bit more happens. Specifically, your code is first compiled to something called “byte code” and then it is passed to a “virtual machine”.

Byte code is basically a low-level representation of your source code. The source code is translated to byte code because byte code can run much more quickly than the original source code.

The interpreter does not expose this translation process to the Python programmer.

Now, this newly generated byte code goes off for execution to the Python Virtual Machine (PVM). PVM is not a separate program, it is in fact always present as a part of Python’s system.

Python Virtual Machine is the runtime engine of Python. It goes through your byte code instructions one by one and carries out the operations stated in those instructions.

Technically, the execution of your byte code by PVM is the ‘Python Interpreter’ last step.

Summary

Here we came to the end of our Python Interpreter article.

In this article, we learned how Python Interpreter executes a program written in Python. Unlike compiler, an interpreter executes the program line-by-line. We also saw how the interpreter takes care of all the internal execution details for us.

That was all about the working of Python’s interpreter.