Python Use Eval Function Evaluate Strings Python Expressions Commented Section File Explai Q37022303

IN PYTHON

  1. Use the eval() function to evaluate these strings as Pythonexpressions. In a commented section of your file, explain what theresults are, which ones result in an error, and why that erroroccurred.

a. ‘2 * 3 + 1’

b. ‘hello’

c. “‘hello’ + ‘ ‘+ ‘world!'”

d. “‘ASCII’.count(‘I’)”

e. ‘x = 5’


Answer


eval() – Evaluates the code given in string format to it.

a). eval(‘2*3+1’)

This evaluate the expression 2*3+1 which 2 multiplied by 3 andadded with 1. So the answer is 7.

b). eval(‘hello’)

Here there is no expression, but only single word hello isgiven. So compiler looks for a variable called hello, which is notdefined. So

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.