Is Python a compiled language or an interpreted language?

Hi, I need to know is python compiled language or an interpreted language

1 Like

Google is our best friend.

image

1 Like

Hi Jhon, you asked:

“is python compiled language or an interpreted language”

Short answer: it is both. It’s an interpreter with a compiler. But if
you can only give one answer, it is more of an interpreted language than
a compiled language.

Longer answer:

When people talk about “compiled languages”, they often think about
languages like C. In C, the process of running a program looks something
like this (simplified):

  • you write the source code;

  • you use a compiler which turns your source code into
    machine language that the CPU of your computer understands;

  • every different family of CPUs uses a different machine language,
    so computer architectures (e.g. Windows versus Mac) needs a
    different compiler;

  • but once you have the compiled machine language, you no longer need
    either the source code or the compiler;

  • on the other hand, the compiled code will only run on one family
    of machines, so you need a separate Windows and Mac version of
    your program.

When people talk about “interpreted languages”, they usually are
thinking of something like this:

  • you write the source code;

  • you use an interpreter which looks at the source code and
    directly executes the statements, without compiling it to
    machine language first;

  • so to run code in an interpreted language, you must have both
    the source code and the interpreter;

  • and a single program (the source code) will run on any machine
    with the right interpreter.

Python, like most languages for, oh, about forty years, combines
elements of both:

  • you write the source code;

  • when you use the interpreter, it first compiles the source
    code into “byte code”;

  • unlike machine code, which is specific to a family of CPUs and
    machine architectures, the byte code is independent of what sort
    of machine you are using;

  • the interpreter then has a “virtual machine” which knows how to
    run the byte code; think of it as a simulated CPU;

  • like compiled languages, there is an intermediate form (the byte
    code) between what you write (source code) and what gets run by
    the actual CPU;

  • so you can compile Python code into a .pyc file and then run that
    compiled version instead of the source code;

  • but like interpreted languages, the byte code doesn’t run directly
    on the CPU of your computer; you still need the interpreter to
    run the byte code;

  • which means that the same .py or .pyc file can be used on any
    machine that has the right interpreter;

  • some Python interpreters, like PyPy, include a “Just In Time”
    compiler which turn parts of the byte-code into machine language
    as needed, for extra speed, without losing the advantages of
    having an interpreter.

These days, most modern languages have elements of both. Interpreters
nearly always have an intermediate compiled form that runs in a virtual
machine; compilers sometimes have a built-in interpreter.

4 Likes

Thanks, @steven.daprano. Your explanation is very educative.

compilers sometimes have a built-in interpreter.

Reminds me of Java, which has jshell as its REPL.

Python and Java both have “bytecode”, though they treat it in vastly different ways.