Table of Contents
The task is to execute the python code placed inside the string variable and print the output.
Python gives us the flexibility to run our code inside the code i.e. dynamic execution of code. For this we have exec() function given by python.
def exec_pycode(): LOC = """ def sq(x): mul = x*x return mul print(sq(5)) """ #Execute python function exec(LOC) exec_pycode()
In the above code, we have placed the python code inside the variable LOC and passed it as the argument to exec() function. The output of inside python code after execution is stored in variable LOC and displayed on the screen using print function.