Let’s Learn: eval() in Python

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"))


Without eval()

>>> type("len")

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).
#Python3 Solution by VK
N = input()
class C:
@staticmethod
def m(N):
return eval(N)
if __name__ == "__main__":
try:
j = C()
j.m(N)
except IOError as e:
print("I/O error({0}): {1}".format(e.errno, e.strerror))
raise

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s