Advertisement

Interpreter and compiler | Difference between Interpreter and Compiler | Python Interpreter

Interpreter and compiler


        Python is a high-level programming language intended to be easy for humans to code and for computers to read and process. Other high-level languages include Java, C++, PHP, Ruby, Basic, Perl, JavaScript, and many more. The actual hardware inside the CPU does not understand any of these high level programming languages.


        The CPU only understands machine languages. Machine language is very simple and frankly very tiresome to write (very difficult for humans) because it consist of only zeros and ones:


00101000110000100101010000001111
111001101000010101010010101101101 ...  

        Machine language looks quite simple on the surface, given that this only consist of zeros and ones, but its syntax is even more complex/difficult and far more complicated than Python. So may be few smart  programmers ever write code in machine language. 


        Since machine language is linked to the computer hardware, machine language is not portable across various types of hardware. Programs written in high level languages can be shift between various computers by using a different interpreter on the new machine or recompiling the code to create a machine language version of the program for the new/other machine.


These programming language translators divided into two general categories:

(1) interpreters.
(2) compilers.


difference between interpreter and compiler



Interpreter


        An interpreter reads the source code of the program as written by the programmer, parses the source code, and interprets the input on the fly. Python is an interpreter and when we are running Python actively, we can write a line of Python and Python execute it immediately and is ready for us to type another line of code. 


        Some of the lines of Python tell Python that you have to remember some value for later. We have to pick a name for that value to be remembered and we can use that unique name to retrieve the value when we need. We call it variable to refer to the labels we use to refer to this stored data


a = 3
print(a)
Output: 3

b = a+5
print(b)
output: 8

        In this example, we ask Python to remember the value three and use the label (a) so we can retrieve the value later. We verify that Python has actually remembered the assigned value using print function. Then we instruct Python to retrieve (a) and sum it by five and put the new value in (b). Then we call print() to show the value of (b) which is 8.


        Even though we are writing these commands into Python one line at a time, Python is treating these lines as an ordered sequence of statements with later statements able to retrieve data generated in earlier statements.


        It is the nature/job of an interpreter to be able to have an interactive conversation as shown in above example.


Compiler

 
        A compiler needs to be handed the entire program in a file at once, and then it execute that process to translate high-level source code into machine language and then the compiler keep the resulting machine language code into a file for later execution.


        If you have a Windows system, often these executable machine language programs have a suffix of “.exe”(executable) or “.dll”(dynamic link library). In Linux, there is no suffix that uniquely marks a file as executable. 


        It is not easy for humans to read or write machine language, so it is useful that we have interpreters and compilers that makes easy to us to write in high-level languages like Python or C.


Python Interpreter


        Now at this point in our discussion about compilers and interpreters, you should be wondering a bit about the Python interpreter itself, that in what language is it written in? Is it written in a compiled language? When we type “python”, what actually is happening? 


     The Python interpreter is written in "C" programming language which is also a high level programming language. By going to this website www.python.org, you can look at the actual source code for the Python interpreter. When we installed Python on our computer, we copied a machine-code copy of the translated Python program onto our system. 



to learn how String variable works in python click here

Post a Comment

0 Comments