Why Python is an Interpreted Language?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Python is one of the most popular interpreted languages, but have you ever thought about why Python is called an interpreted language while other programming languages like C, C++, Java, etc., generate results after compilation? So, you might be curious about what is this interpreted language. And how is this different from the language which generates results after compilation?

To answer the question, we must know what interpreted means.

Interpreted in simple terms means running code line by line. It also means that the instruction is executed without earlier compiling the whole program into machine language.

Now, let us discuss how Python works as an interpreted language. Consider a scenario where you are trying to run a python code, but unfortunately, you have made some mistakes at the bottom of the code. You will find that there is an error generated for obvious reasons, but along with the error, you will find the output of the program till the line of the program is correct. This is possible because Python reads the code line by line and generates output based on the code. Whenever it finds any error in the line, it stops running and generates an error statement.

Python is Both Compiled as well as Interpreted

"Python is an interpreted language", is the most common saying, which is also written in various books, but the hidden fact is Python is both compiled as well as an interpreted language. This means when we run a python code, it is first compiled and then interpreted line by line. The compilation part is mostly hidden from the user. While running the code, Python generates a byte code internally, this byte code is then converted using a python virtual machine (p.v.m) to generate the output.

Now, let us try to prove the fact python is both compiled as well interpreted.

Note: The compile part gets deleted as soon as the code gets executed so that the programmer doesn't get onto unnecessary complexity.

Take a sample code

Now, save this code with a program name with .py as the file extension. For example,

save this code with a program namepy

Let trial101.py be the name of the python file.

Now, open the terminal and try running the trail101.py.

You will see a folder named pycache is being generated. Which contains a file named trial101.cpython-311.pyc, which is byte code generated after compilation.

open the terminal and try running the trail101py

Finally, when we run the byte code-named, trial101.cpython-311.pyc is executed, we get,

execution of byte code-named trial101cpython-311pyc

Thus, we can verify that the python program is first compiled and then interpreted.

Advantages of Interpreted Languages

An interpreted language gives some extra benefits and flexibility over compiled language.

  • Since the interpreter reads instructions line by line and generates output till the point the code is correct, the ease of debugging increases as it is easier to get information about the source point of error.
  • The size of programs written in Python is less as compared to other languages.
  • As Python generates byte code before interpretation, this byte code can be used by any other platform to generate output.

Disadvantages of Interpreted Languages

  • A program that is executed in an interpreted language is slower as compared to a language that is directly compiled.
  • It happens because the line of codes passes through an interpretation run-time.
  • The code has to be compiled, and after compilation, a byte code file is generated before interpretation which makes the execution time high. Therefore due to this problem, the run-time complexity of the program increases.

To learn further about Python, you can refer to what-is-python?

Conclusion

  • Python is both compiled as well as an interpreted language, which means when we run a python code, it is first compiled and then interpreted line by line.
  • The compile part gets deleted as soon as the code gets executed in Python so that the programmer doesn’t get onto unnecessary complexity.
  • The size of programs written in Python is less, and it is easier to debug the code in the Python language.
  • The program that is executed in an interpreted language is slower as compared to a language that is directly compiled because the line of codes passes through an interpretation run-time which increases the run-time complexity.