How to create calculator in Python the eval() method

Pybeginner
By -
0



Here, we will create a Calculator in Python using the eval() method. 

This will be a good exercise for practicing the predefined methods that Python offers.

As always, open your text editor and create a new Python file.

The calculator will be created using the eval method as I said.

eval() is a built-in function used in python, the eval function parses the expression argument and evaluates it as a python expression. In simple words, the eval() function evaluates “String” as a python expression and returns the result as an integer.

 

# function
def calculate(a):
result = str(eval(a))
print(result)

value = str(input("Enter the expression: "))

calculate(value)

 

Output:

 

Enter the expression: 6+5
11

Post a Comment

0Comments

Post a Comment (0)