A common question: “Is Python compiled, Interpreted, or both?”.
Types of Programming Languages
Any high-level programming language convert to machine language in two ways as
Convert high-level program to its machine or CPU instruction sets I,e machine bytecode. Therefore, the compiler checks for its syntax first and convert the whole program to machine or CPU understandable bytecode.
A compiled language is a programming language whose implementations are typically compilers (translators that generate machine code from source code). And interpreters are step-by-step executors of source code, where no pre-runtime translation takes place.
Ex: c, c++, Java
Interpreters work differently; they take each expression or line of the program and convert to machine code and execute it. Hence, if there is an error in a specific line, it’ll be shown at the time of execution.
An interpreted language is a type of programming language for which most of its implementations execute instructions directly and freely. Without previously compiling a program into machine-language instructions. The interpreter executes the program now, translating each statement into a sequence of one or more subroutines and then into another language (often machine code).
Ex: Unix shell
Is Python Interpreted?
It is a bit strange in terms of compilation, coming to Python, remember this word.
Python is a “COMPILED INTERPRETED” language.
This means when the Python program is run,
- First Python checks for program syntax
- Compiles and converts it to bytecode, and directly bytecode is loaded in system memory.
- Then compiled bytecode interpreted from memory to execute it.
Whereas other languages like c convert programs to machine code and save them as executables in the disk. And then the user can run it as a.out
We usually call Python an interpreted language because the compilation happens behind the scene and when we run the python code through:
python Hello.py -> directly executes the code, and we can see the output provided that code is syntactically correct
python Hello.py -> it looks like it directly executes, but it first generates the bytecode that the interpreter interprets to produce the native code for the execution purpose.
CPython takes the responsibility of both compilation and interpretation.
Proof for Python compilation is when you import any Python module in another program. In the imported module directory, another file with the same name .pyc will generate. This file executes later on.
Though some believe and say Python is an interpreted language, it is not. The compilation happens behind the scene, and when we run the python code through the terminal, it gets converts a compiled file.
To summarize, Python is an interpreted language, unlike other programming languages. All the errors are displayed when the specific line containing the error is compiled.