I know that the function eval executes the string(in the parameter) without the quotes .So what I was just trying was that
x=eval("4*5") #this works
eval("x=4*5") #but this doesn't
Please tell where I am wrong!
I know that the function eval executes the string(in the parameter) without the quotes .So what I was just trying was that
x=eval("4*5") #this works
eval("x=4*5") #but this doesn't
Please tell where I am wrong!
4*5
is an expression, it has a value of 20
.
x=4*5
is not an expression and it doesn't have a value. It's a statement, meaning it performs an action.
eval()
runs its string parameter as a python expression. It evaluates the value of that expression. It cannot work with assignments or other statements that are not expressions.
eval
function has the following structure:
eval(expression, globals=None, locals=None)
The expression argument is parsed and evaluated as a Python expression. It is actually a string expression rather than a assignment.
Ref: Here