eval() function in python by R4R Team

- eval() is a special type of function in python, which execute itself within the program.
-It is used to solve the mathematical expression(equations)

Syntax-

eval(expression,global=0,local=0)

-Here global and local are optional part of the eval.

Example-

expr="x**2+x*2+6"
and x is 2
then eval(expr) will return 14


program-

y='x**2+x*1+1'
x=3
z=eval(y)
print(z)

y='x**3+x*1+1'
x=2
z=eval(y)
print(z)

y='x*1+1'
x=5
z=eval(y)
print(z)


output-

13
11
6


-In this program, we have a expression in string format i.e. 'x**2+x*2+6' and where value of x is 3 in first case then eval(expr) will become eval(x**2+x*1+1) which return 13, similalry other works.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!