Roots of Quadratic Equation in Python

Quadratic equations are second-degree algebraic expressions and are of the form ax2 + bx + c = 0. The word “Quadratic” is derived from the word “Quad“, which means square. In other words, a quadratic equation is an “equation of degree 2.”

There are many scenarios where a quadratic equation is used. Did you know that when a rocket is launched, its path is described by a quadratic equation? Further, a quadratic equation has numerous applications in physics, engineering, and astronomy. The quadratic equations are second-degree equations in x that have two answers for x. These two answers for x are also called the roots of the quadratic equations and are designated as (α, β). We shall learn more about the roots of a quadratic equation in this blog.

What is the Quadratic Equation?

We have studied this term in our school and college days. A quadratic equation is an algebraic expression of the second degree in x. The standard form of a quadratic equation is ax2 + bx + c = 0, where a, and b are the coefficients, x is the variable, and c is the constant term.

The first condition for an equation to be a quadratic equation is that the coefficient of x2 is a non-zero term(a ≠0). For writing a quadratic equation in standard form, the x2 term is written first, followed by the x term, and finally, the constant term is written.

The numeric values of a, b, and c are generally not written as fractions or decimals but are written as integral values. Further, in real math problems, the quadratic equations are presented in different forms: 

(x – 1)(x + 2) = 0, -x2 = -3x + 1, 5x(x + 3) = 12x, x3 = x(x2 + x – 3)

All of these equations need to be transformed into the standard form of the quadratic equation before performing further operations. We want you to learn about the roots of quadratic equations in python; hence we are come up with the best video explanation you have ever watched on the internet, where you not only understand the logic but also code to get the desired output.

Do you still face difficulties in understanding the logic? That’s perfectly fine; we have something more for you. You can scroll down to learn more about this topic.

For all the practice Videos and Explanations on Python, please click over here. Python Practice Series.

Roots of a Quadratic Equation in Python with Details Explanation

The roots of a quadratic equation are the two values of x, which are obtained by solving the quadratic equation. The roots of a quadratic equation are referred to by the symbols alpha (α) and beta (β). These roots of the quadratic equation are also called the zeros of the equation.

Here we shall learn more about how to find the nature of the roots of a quadratic equation without actually finding the roots of the equation. And also, check out the formulas to find the sum and the product of the roots of the equation.

Nature of Roots of the Quadratic Equation

The nature of the roots of a quadratic equation can be found without actually finding the roots (α, β) of the equation. This is possible by taking the discriminant value, which is part of the formula to solve the quadratic equation. The value b2 – 4ac is called the discriminant of a quadratic equation and is designated as ‘D’. Based on the discriminant value, the nature of the roots of the quadratic equation can be predicted.

The logic for Roots of Quadratic equation

The following list of important formulas is helpful for solving quadratic equations.

  • The standard form of a quadratic equation is ax2 + bx + c = 0
  • The discriminant of the quadratic equation is D = b2  – 4ac
  • For D > 0, roots will have two values

x = (−b±√b2−4ac)/2a

  • For D == 0 roots will have one value

x = (-b)/2a

  • For D < 0 roots are imaginary.

Let’s start with a program to find the roots of a quadratic equation.

Program: Roots of quadratic equation in Python

Source Code and Output

import math

a = float(input("a:"))
b = float(input("b:"))
c = float(input("c:"))

delta = math.pow(b,2)-(4 * a * c)

if delta > 0:
    #number of roots are 2
    x1 = (((-b) + math.sqrt(delta)) / (2 * a))
    x2 = (((-b) - math.sqrt(delta)) / (2 * a))
    print("There are 2 roots:%f and %f" % (x1, x2))
elif delta == 0:
    #number of roots 1, rational and equal
    x = (-b) / 2 * a
    print("There is one root:", x)
else:
    #number of roots 0, imaginary
    print("No roots, delta < 0")
Output:
a:2
b:5
c:1
There are 2 roots:-0.219224 and -2.280776

Video Explanation of Roots of a Quadratic Equation in Python

Code Explanation: Roots of quadratic equation in Python

In this program, we are using python’s Math Library for mathematical calculations. So in the first line, we have imported a math library. Then in the next line, we are accepting the value of the equation coefficient, which is nothing but a, b and c, from the equation with input function, and we will convert them into floats and store them into variables a,b and c.

Now we have all the required details to calculate the delta term. So, let’s start with that. Here we are using math’s pow function to calculate the square of b, so we have declared delta, and we are assigned math.pow(b,2) here, we are providing b and power to pow function minus 4*a*c.

Then we will check the value of the delta. If the delta is greater than 0, then we want two root values; hence we are storing 1st equation value into x1 and another into x2. Then we will print the result here with two values with a print statement. Then we will write elif to check if the delta is 0; in this case, the delta has one value. For check root, we will write (-b) / 2 * a and store the result into variable x, and in the next line, print the result.

And now, in the else section, we will print that no root is available as the delta is less than 0. Let’s check after running this program. We will enter 2,5,1 for a,b and c, respectively, and see we have our output with 2 root values -0.219224 and -2.280776.

I hope now you know how to check the roots of quadratic equations.

About The Author