Python is said to be interpreted because it is interpreted by an interpreter at runtime. The code is literally read line by line and executed this way.
The Python interpreter is a software program that reads Python code, interprets it, and executes it. So we could think of Python code as pseudo compiled in the sense that the interpreter interfaces the compiler.
Python -> Interpreter -> Compiler
Since the source code is interpreted at runtime, you can usually run a Python program on any platform that has a Python interpreter, without the need to recompile the code for each platform.
Interpreted languages tend to be slower than compiled languages, since the interpreter has to read and execute the source code at runtime. This can make interpreted languages less suitable for tasks that require a lot of processing power, such as data analysis or scientific computing.
Despite these limitations, Python is a popular and widely used interpreted language that is used for a variety of tasks, including web development, data analysis, machine learning, and more.
Put the code inside a function.
# python code runs faster inside a function !
import dis
def my_function():
''' this is a function '''
for i in range(100):
print(i)
return
dis.dis(my_function)
Why does this work ?
Because the variables inside the function are local and they "load fast" in the byte code compared to variables which are outside of the function which are in the global space.
So whist the code actually looks like it might take longer in the basic example above (because there are simply more lines of code), it is actually more efficient to write code inside of the function.
Here is an interesting link:
https://www.youtube.com/watch?v=GNPKBICTF2w&ab_channel=EuroPythonConference