Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to compute arithmetic operation from String
Arithmetic operations are mathematical calculations performed on numeric data types. Python supports several arithmetic operators that can be computed from string expressions.
Addition (+)
Subtraction ()
Multiplication (*)
Division (/)
Floor Division (//)
Modulo (%)
Exponentiation (**)
There are several ways to compute arithmetic operations from strings. Let's explore different approaches with their advantages and limitations.
Using the eval() Function
The eval() function evaluates a string expression and returns the result. This is the simplest approach but should be used cautiously due to security risks.
Example
The eval() function directly evaluates the mathematical expression and follows proper operator precedence ?
def compute_operation(expression):
result = eval(expression)
return result
expression = "2 + 3 * 4 - 6 / 2"
result = compute_operation(expression)
print("The result of the given expression:", result)
The output of the above code is ?
The result of the given expression: 11.0
Using Custom Parsing (Left-to-Right Evaluation)
For more control and safety, we can implement custom parsing. This approach evaluates expressions from left to right without considering operator precedence.
Example
This method splits the expression and processes operators sequentially ?
def compute_operation(expression):
operators = {'+': lambda x, y: x + y,
'-': lambda x, y: x - y,
'*': lambda x, y: x * y,
'/': lambda x, y: x / y}
tokens = expression.split()
result = float(tokens[0])
for i in range(1, len(tokens), 2):
operator = tokens[i]
operand = float(tokens[i+1])
result = operators[operator](result, operand)
return result
expression = "2 + 3 * 4 - 6 / 2"
result = compute_operation(expression)
print("The result of given expression:", result)
The output of the above code is ?
The result of given expression: 7.0
Using the operator Module
The operator module provides functions corresponding to built-in Python operators, offering a cleaner and more readable approach.
Example
This approach maps string operators to their corresponding functions from the operator module ?
import operator
def compute_operation(expression):
ops = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv,
}
tokens = expression.split()
result = float(tokens[0])
for i in range(1, len(tokens), 2):
operator_func = ops[tokens[i]]
operand = float(tokens[i + 1])
result = operator_func(result, operand)
return result
expression = "10 + 5 * 2"
result = compute_operation(expression)
print("The arithmetic operation result:", result)
The output of the above code is ?
The arithmetic operation result: 30.0
Comparison
| Method | Security | Operator Precedence | Best For |
|---|---|---|---|
eval() |
Low (security risk) | Yes | Trusted input only |
| Custom Parsing | High | No | Simple left-to-right evaluation |
| operator Module | High | No | Clean, readable code |
Conclusion
Use eval() only with trusted input due to security risks. For safer alternatives, implement custom parsing or use the operator module. Choose based on whether you need operator precedence or prefer left-to-right evaluation.
