This question already has an answer here:
After reading query.
below python code is still not clear,
>>> exec('print(5+10)')
15
>>> eval('print(5+10)')
15
In bash
world,
exec
replace the shell with the given command.
eval
execute arguments as a shell command.
Question:
Expression is a computation that evaluates to a value
To evaluate any expression in python(in my case print(5+10)
from above python code), How eval()
works different from exec()
?
In your two cases, both
eval()
andexec()
do, do the same things. They print the result of the expression. However, they are still both different.The
eval()
function can only execute Python expressions, while theexec()
function can execute any valid Python code. This can be seen with a few examples: