How to Solve Quadratic Equations in Python using the Sympy Module


In this article, we show how to solve quadratic equations in Python using the Sympy Module.

Quadratic equations are used to solve trinominal or binomial mathematical equations.

The general formula for a quadratic equation is, ax2+bx+c=0

If the value of b is 0, then the equation is a binominal and takes the form ax2+b=0

Solving for the values of x in a quadratic equation yields 2 values, called the root of the quadratic equation.

Quadratic equations always yields 2 values as its roots, or answer.

This rises from the fact that the formula to solve a quadratic equation is, x= -b ± √b2 - 4ac/(2a)

The ± is the reason there are 2 roots. One is obtained by the plus (addition). The other is created by the minus (subtraction).

So in order to solve for the roots of a quadratic equation, we use the solve function from the sympy module.

The sympy module is a module in Python that is rich with mathematical capability, one of which is solving equations, including quadratic equations.

We use sympy to solve for quadratic equations.

So in order to solve for quadratic equations, we must import from the sympy module Symbol and solve.

x is the most common variable in mathematics, so we use x as the variable for our quadratic equation.

We then create a variable, expression, which we set equal to a quadratic equation. In this case, we set it equal to, x**2+7*x+6, which is x2+7x+6.

In Python, ** is equal to raisining a variable or number to a certain power. In this case x is raised to the second power.

We then create another variable, roots, which we set equal to the solve() function. This solve() function takes in 2 parameters. The first parameter is the quadratic equation. The second parameter is the whether the dict, which can be set equal to True or False.

If dict is set equal to false, then the roots of the quadratic equation will be put in a list, composed of 2 elements, the 2 roots of the quadratic equation.

If dict is set equal to True, then the roots will be placed in a list as well but with a dictinoary format. We show this in the example below.

Being that we set, dict=False, in this example, the roots are in a list not in dictionary format. To reference the 2 roots in our list, we use, roots[0] and roots[1]

Below we show the same exact code but with the dict set equal to True.

So we have all the same code except now in the solve() function, we set, dict=True

This creates a list as well, but with a dictionary format. You see the key-value pair together. Being that it's not actually a dictionary, the key-value pair are not separable, as it would be with a true dictionary. If you want the variable appearing with the value, you can use the solve() function with dict set equal to True. Otherwise, if you just want the value of the root, set dict equal to False.

And this is how we can solve quadratic equations in Python using the sympy module.

Related Resources

How to Extract All Files and Folders from a Zip File in Python
How to Read the Contents of a Zip File in Python

HTML Comment Box is loading comments...