How does python work? how python is interpreted?

By: On:

Welcome!! In this Python tutorial, We will discuss Introduction to Python, How does Python work? Is Python interpreted, or compiled, or both? how python is interpreted? What is the Python Virtual Machine?

Introduction to Python

In simple words, Python is an open-source object-oriented programming language like C#.Net, Java, C++, etc.


Python has very simple syntax, simple data structures, a basic set of data types, support to many standard libraries so it is a better choice for beginners and experienced programmers. Please refer to a tutorial on What is Python?

How does Python work?

Lots of people search for how does python work? You will get an idea here.

Python is an object-oriented language like C++, C#, etc. Behind the scene, When you are writing any program in C++ or C#, java. What happens exactly?

When you are compiling the source code actually the compiler internally converts the source code to machine level code or machine code which machine can able to understand and can give you the output accordingly.


So compiler acts as an intermediate between your source code(Human understandable language) to convert to machine code (The language which machine can understand).

Now in case of Python, The compilation also happens internally but here the source code is converted to “byte code” not machine code directly like other languages.

Python virtual machine(PVM) will take this “byte code” as an input and it processes the same to machine code or machine understandable code and use to give the output in case there is no error in the processed code or else if there are any error presents it will prompt with the error message.

Is Python interpreted, or compiled, or both? how python is interpreted? can python be compiled?

Actually, When python code runs, it first gets complied by python and generates the byte code. So python code first gets compiled and then after it gets interpreted.

Then after the byte code is processed as an input to Python virtual machine(PVM) and PVM process the byte code to machine code and get you the output if in case there is no error in the processed code. If incase there are any errors in the processed code PVM will prompt with the error message.

The compilation of python code happens internally and it’s not visible to the real world.

The .py source code is first compiled to byte code as .pyc. Later on this byte code used to get interpreted.

Python used to create a file with .pyc extension internally when you are trying to execute any of the .py files. So .pyc is nothing but Python compiler.

This is the reason Python is also known as an interpreted (byte code-compiled) language.

So, basically Below are the steps for the whole story

  • Step-1: User provides the input as the python code or program
  • Step-2: Python compiles the python code to “Byte code”.This happens internally it’s not visible to the real world.
  • Step-3: Now the next major role is for Python virtual machine (PVM) which processes the “Byte code” to “Machine code” or “Machine level code”(which machine can able to understand) internally and give us the output.
How does python work

Some important things to note here, if Python virtual machine (PVM) finds any error while processing the byte code then it will prompt with the error message and will stop the execution there itself. But if it will not find any error or if the byte code is error-free then PVM will provide you the desired output.

What is the Python Virtual Machine (PVM)?

Python virtual machine(PVM) is also known as “Python virtual memory” which is nothing but a software which is playing a vital role while executing the python raw code.

When user give the python raw code as a input or user is trying to execute the python code, Python will compile the raw code to “byte code” internally.

Now the actual work of the PVM starts, so it will process the “byte code” to the “machine code” which machine can understand and then it will get you the actual output if there are no error exists on the code.

Conclusion

Python is the most popular open-source object-oriented programming language. Python is easy to learn and syntax wise it is very simple.

Now in case of Python, The compilation also happens internally but here the source code is converted to “byte code” not machine code directly like other languages.

Python virtual machine(PVM) will take this “byte code” as an input and it processes the same to machine code and use to give the output in case there is no error in the processed code

Python is the best choice for many of the top most companies.

Python is the best!!!!!


You may like following Python tutorials:

This python tutorial explains the below points:

  • Introduction to Python
  • How does Python work?
  • Is Python interpreted, or compiled, or both? how python is interpreted? can python be compiled?
  • What is the Python Virtual Machine?

Leave a Comment