What is an interpreter?
Interpreter is a software which is used to run programs written in Python language. Computer does not understand human language. Computer understands machine language i.e. 0 and 1, hence interpreter explains the correct meaning of our programs to the computer in machine code (0 and 1).
How does an interpreter work ?
You already know that Interpreter explains our written programs to the computer. To do this,
Interpreter follows some steps which are defined below : -
Step 1. Whenever a program is written, Python Interpreter reads that code line by
line
and Interpreter converts that code into machine code.
Step 2. When Interpreter converts the code into machine code i.e. Bytecode , then it
gives this Bytecode to PVM (Python Virtual Machine) .
Step 3. After this PVM executes/runs the entire code line by line and gives the
output.
What is a compiler ?
Computer does not understand human language, it understands 0 and 1, hence to explain the codes / programs written in any programming language to the computer, an interpreter or compiler is used. Compiler is a computer program which is used to convert the programs written by us into machine code. Compiler works exactly opposite to the interpreter, that is, instead of executing the written programs line by line, the compiler reads the entire program at once.
How does the compiler work ?
1. Source Code = > This is the first step in which the code is written by the
programmer,
that is, the source code is what the programmer writes.
2. Scanner = > This is the second step in which the compiler divides the source code
into
small parts like: int → keyword a → identifier, = → operator. If there is any spelling
related
mistake in this step, then the compiler gives an error.
3. Syntactic Analysis = > This is the third step in which the compiler checks the
errors
related to grammar, declared variables, etc. in the written program. If any error is found,
the
output of the entire program is not available.
4. Generate Code = > This is the fourth step of the compiler in which the compiler
makes
the code better and faster so that it takes less memory.
5. Machine Code = > This is the last step of the compiler in which after completing
all
the steps, the compiler converts the codes written by the programmer into machine code (0
and 1)
which the computer can run directly.
Difference between Interpreter and Compiler
| Interpreter | Compiler |
|---|---|
| 1. Line By Line execution | 1. Full Program Execution |
| 2. Execution Slow | 2. Execution Fast |
| 3. Direct Run | 3. Compile Run |
| 4. Does not generate any file. | 4. Generates .exe file. |
| 5. C / C++ | 5. Python , JavaScript |
What kind of language is Python ?
Python is an interpreted language because code execution in Python happens line by line,
hence Python is called an interpreted language but if seen technically then Python is a
combination of both interpreted and compiled, in such a case Interpreter does not create
any extra file for Machine Code, whereas Compiler creates an extra file. When we write
Python code, it is not run directly. The execution process of the written program happens
like this.
.py (source code) → bytecode (.pyc)
This process is called Compilation. And .pyc file is created in the process, in this way
Python is a compiled language. But code execution happens line by line, hence Python is both
interpreted and compiled language.
NOTE: Python is neither a fully interpreted language nor a fully compiled language
Python = (Compiled + Interpreted)