
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Compute Arithmetic Operation from String in Python
Arithmetic operations are the mathematical calculations on numeric data types. The following are the arithmetic operations allowed in python.
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Floor Division (//)
Modulo (%)
Exponentiation (**)
There are several ways to compute arithmetic operation from string. Let's see them one by one.
Using the eval() Function
The eval() function in Python evaluates an expression passed as a string and returns the result. We can use this function to compute arithmetic operations from a string.
Example
In this approach, the eval() function evaluates the expression "2 + 3 * 4 - 6 / 2" and returns the result, which is then stored in the variable "result".
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)
Output
The result of the given expression: 11.0
Implementing Arithmetic Parsing and Evaluation
If we want more control over the parsing and evaluation process, we can implement our own arithmetic parsing and evaluation logic. This approach involves splitting the string expression into individual operands and operators, parsing them, and performing the arithmetic operations accordingly.
Example
In this example, the expression is split into individual tokens using the split() method. The tokens are then parsed and evaluated iteratively according to the arithmetic operators specified in the operators dictionary. The result is computed by applying the appropriate operator to the accumulated result and the current operand.
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)
Output
The result of given expression 7.0
Using the operator module
In python we have the operator module, which provides functions corresponding to built-in Python operators. We can use these functions to perform arithmetic operations based on the operators present in the string expression.
Example
In this example, we define a dictionary that maps the operators to their corresponding functions from the operator module. We split the expression into tokens, where operators and operands are separated. Then, we iterate through the tokens, applying the corresponding operator function to the result and the next operand.
import operator expression = "2 + 3 * 4" ops = { '+': operator.add, '-': operator.sub, '*': operator.mul, '/': operator.truediv, } tokens = expression.split() result = int(tokens[0]) for i in range(1, len(tokens), 2): operator_func = ops[tokens[i]] operand = int(tokens[i + 1]) result = operator_func(result, operand) print("The arithmetic operation of the given expression:",result)
Output
The arithmetic operation of the given expression: 20