Python Forum

Full Version: Solve simple equation in Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I am fairly new to working with mathematics in Python and I am wondering how I can use Python so solve an eqations like this:

0.4x - 0.38 = 0.58

Thanks
For solving symbolic math equations take a look at SymPy
Or use basic algebra to solve for x then print the result

0.4x - 0.38 = 0.58
0.4x = 0.58 + 0.38 = 0.96
x = 0.96/0.4

print(0.96/0.4)
Output:
2.4
(Nov-01-2020, 03:43 PM)deanhystad Wrote: [ -> ]For solving symbolic math equations take a look at SymPy

Thanks for the advise. But I did and I did not quite understand how I can use if for an equation similar to the one I listed above so I was hoping for a concrete solution/example.
I have never used SymPy myself, but from this tutorial I think it looks fairly straight forward:

https://pythonforundergradengineers.com/...tions.html

My first shot would be:
from sympy import symbols, Eq, solve
x = symbols('x')
eq = Eq('0.4x - 0.38 - 0.58')
solution = solve((eq), (x))
There may be a way to solve an equation without first putting it in the form equation = 0. I don't know.