Python is an interpreted language. What does that mean? See the image below.
Actually, the Python code we create cannot be read or executed directly.
This happens because Python is a high-level language that makes it easier to read and write by humans.
However, computers can only read or execute commands using machine language, so there is a part that the computer must go through when running a Python program. This part is called the interpreter. When the program runs, the interpreter acts as an intermediary that translates our Python file into commands that the machine can understand and provides output to our terminal.
To make it easier to understand, check the illustration below.
It is executed from the first line and so on downwards.
- Blank lines will be skipped.
- Comments will be skipped.
- Whitespace (spaces, tabs, and newlines)
However, Python can also be a compiler language. See the illustration below.
A compiler is computer software that converts a program’s source code in a programming language into object code or machine language that the computer can understand and then execute.
What is the difference between compiler and interpreter?
In a compiler, the entire program code is translated first before being executed by the computer. In an interpreter, the program code is converted to machine language directly when the program is run.
How does a compiler work?
The way a compiler works is by reading the program’s source code, checking the program’s syntax, converting it into object code, and then linking the object codes into a file that can be executed by the computer.
What is the extension of the program file after the compilation process?
The extension of the program file produced from the compilation process in a compiler depends on the programming language and platform used. For example, the file extension .exe for Windows, .out for Linux and .apk for Android.
Conclusion
Python is an interpreted high-level programming language, which means its code cannot be directly read or executed by a computer. Instead, an interpreter translates the code into machine language line by line as the program runs. This makes Python easier for humans to read and write compared to lower-level languages.
In contrast, compiled languages use a compiler that translates the entire source code into machine language before execution, producing an executable file. The main difference between interpreters and compilers lies in the timing and method of translation. The file extensions for compiled programs vary based on the programming language and platform, such as .exe for Windows, .out for Linux and .apk for Android.