It looks like an easy question to answer, obviously interpreted, because Python code is run line by line at runtime. But it's wrong.
If your are a newbie to programming and have been using a language to code, it might have been obvious to you that some languages are interpreted, some are compiled to machine code, and some are both compiled and interpreted.
For example, when we use the CLI to run
python3 -c "print('Hello World')"
, we see that the code was executed at runtime and hence we assume that all python code is supposed to be interpreted.
Similarly, when we write a C code, we need to compile it to an executable and then run it, and hence we assume that all C code must be compiled.
But the language itself is not either interpreted or compiled. It is not a property of the language. When we see a C code or python code it is nothing but a blot of ink on paper or some random text on a text editor. It is nothing but an idea of concepts in air. Computers cannot understand these text based ideas that humans can, it needs machine code to execute a program.
Hence we need some form of implementation of the language that does the hard work for us. Therefore there exists a Compiler or an Interpreter. It is the compiler's or interpreters job to convert the raw text based code written in any language to machine readable code for execution.
Coming back to the question,
Is python compiled or interpreted?.
Depends. Since most of us use python by the
py command, we are actually using the implementation called CPython written in C. It compiles
.py files to
.pyc files contain byte codes. The CPython implementation also interprets those byte codes.
There are various other implementations for python such as Jython (written in Java) or PyPy (written in RPython). These are just-in-time compilers for python.
Similarly there exists interpreters for languages such as C.
It is not the property of a programming language to be either compiled or interpreted but the implementation of the language in use instead.
Do correct me if I make a mistake in this article. 😊