Solve Math Problems Instantly: Python’s eval() for Speedy String-Based Calculations

For example we have math expression in string like “4+2+34+346-234”
We can directly calculate from this text with eval()

Directly calculate from string


our_expression= "4+2+34+346-234"

answer=eval(our_expression)
print(answer)
# 152


More things we can do

calculate from variables


x = 7
y = 5
expression = "x * y"
result = eval(expression)
print("Result:", result)
# Result: 35


Directly take expression as user input


expression = str(input("Enter a mathematical expression: "))
result = eval(expression)
print("Result:", result)


Also we can use python math library to solve more complex expression


import math

# Define a mathematical expression as a string
expression = "math.sqrt(25) + math.cos(math.radians(45))"

# Evaluate the expression using eval()
result = eval(expression)

print("Result:", result)
# Result: 5.707106781186548


Using with lambda


# Define the lambda function
calculate_and_show_divisors = lambda equation: \
    [i for i in range(1, eval(equation) + 1) if eval(equation) % i == 0]

# Define the equation
equation = "3 * 4 + 2"

# Call the lambda function with the equation
divisors = calculate_and_show_divisors(equation)

print("Divisors of the result:", divisors)
# Divisors of the result: [1, 2, 7, 14]


The considerations

if you are using eval for your personal project that’s OK. but if you are applying it on server on application, there is security considerations. Because it can calculate directly from string, so untrusted user input can be cause of unexpected code execution. So try alternative in this situations or apply user input validation logic.

Practice this examples on browser based free python online interpreter(open source) limitations=user input will not work, and also other limitations

Related Posts

Print with python f string format
July 3, 2024

We use make the python print more wonderful with f string (formatted string) x=1 y=2 z=x+y print(f’result {x}+{y}={z}’) Copy Code this will print We can make it more wonderful directly adding expression x=1 y=2 print(f'{x} times {y} is {x * y}’) Copy Code We can also set the decimal places in the print number = […]

Check if a key exist or not in a python dictionary
July 3, 2024

We can check it with “in” operator and “get()” method In operator here returns the value True or False For example dictionary={“one”:1, “two”:2} if “two” in dictionary: print(“two exist in the dictionary”) Copy Code The get() method returns the value if that key exists. If not exists it returns None dictionary_data={“one”:1, “two”:2} #Here the key […]

Find out median of a list with python
July 3, 2024

Python has wonderful built-in library “statistics” We can find out the median of a list with this library easily with less code import statistics List = [1,2,3,4,5,6,7,8,9,10] result= statistics.median(List) print(result) Copy Code We can also generate list randomly with random module List = [randint(1, 101) for _ in range(50)] this code create a list of […]