Monday, 27 May 2019

eval() function

eval():
Ex1:
a=eval(“10+20+30”)
print(a)
output: 60

Ex2:
>>> a=eval(input("enter any expression"))
enter any expression10+2*3/4
>>> print(a)
11.5
>>> 


Note:
1.   First input() reads the expression i.e 10+2*3/4 in the form of string.
Then the statement looks like this:
a=eval(“10+2*3/4”)
2.   Now eval() evaluates the expression i.e 11.5 and stores into left hand side variable a.
i.e a=11.5

3. now we can print by using print().

1Q: If we omit the eval() in above ex2 program:
>>> a=input("enter any expression")
enter any expression10+2*3/4
>>> print(a)
10+2*3/4
>>> 
Definition: eval the function take a string and evaluate the result.

Related video: https://www.youtube.com/watch?v=-AJntqLTi94&feature=youtu.be

Next: Mathematical Functions
Previous: Input and output statements


No comments:

Post a Comment

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(...