Navbar menu

HackerRank Python Solution - Built-Ins - eval()

  • The eval() expression is a very powerful built-in function of Python. It helps in evaluating an expression. The expression can be a Python statement or a code object.
  • For example:
>>> eval("9 + 5")
14
>>> x = 2
>>> eval("x + 3")
5
  • Here, eval() can also be used to work with Python keywords or defined functions and variables. These would normally be stored as strings.
  • For example:
>>> type(eval("len"))
<type 'builtin_function_or_method'>
  • Without eval()
>>> type("len")
<type 'str'>
Task:
  • You are given an expression in a line. Read that line as a string variable, such as var, and print the result using eval(var).
Constraint:
  • The input string is less than 100 characters.
Sample Input:

print(2 + 3)
Sample Output:

5
Solution:

exp = input()

eval(exp)
Disclaimer: The problem statement is given by hackerrank.com but the solution is generated by the Geek4Tutorial admin. If there is any concern regarding this post or website, please contact us using the contact form. Thank you!

No comments:

Post a Comment