How to solve a quadratic equation in Python

Key takeaways:

  • A quadratic equation is a polynomial equation of degree 2, represented as ax²+bx+c=0ax² + bx + c = 0.

  • Python’s math library is used to calculate the discriminant and find the roots using the quadratic formula.

  • The solution can be validated by substituting the roots back into the original quadratic equation.

In this Answer, we’ll discuss the quadratic equation, its nomenclature, formation, and implementation to solve a quadratic equation in Python.

Quadratic equations in mathematics are simple, and yet can be a bit tricky. A polynomial equation with a maximum power of degree 2 is known as a quadratic equation or equation with degree 2. It might be confusing to call it quadratic, which usually means four, but we use it for equations of power 2. To understand why, we need to look into the origin of the word “quadrant.”

The word “Quadrant” originates from the Latin word “Quadri,” which means four, like quadrilaterals, quadrangles, and quadrants. Now, the area of quadrangles, squares, or quadrilaterals is calculated as x^2 which is a polynomial equation with degree 2. That’s why an equation with a maximum degree of 2 is called a quadratic equation.

Formation of quadratic equation

A quadratic equation is composed of variables, coefficients, and constants. The generic form of a quadratic equation is ax2+bx+c=0ax^2 + bx + c =0 where a and b are coefficients, c is constant, and x is a variable. Here, the coefficient a can’t be 0 because it would make the term ax^2 equals to 0, and the equation will become a linear equation. Here's what the graph of a quadratic equation looks like:

Quadratic equation graph
Quadratic equation graph

Steps to solve a quadratic equation

Before diving into the code, let’s examine the steps in solving the quadratic equation. We’ll use the quadratic formula x=b±b24ac2ax = \frac{{-b \pm \sqrt{{b^2 - 4ac}}}}{2a} to solve the quadratic equation. We’ll determine the following:

  • Discriminant: b±b24ac-b \pm \sqrt{{b^2 - 4ac}}

  • Check if the discriminant is positive, negative, or zero

  • Roots by analyzing the value of the discriminant

Implementation of solving a quadratic equation in Python

The Python code in the widget below shows the code to find the discriminant and then the roots to solve the quadratic equation.

import math
def find_DiscriminantAndRoots(a, b, c):
# 1. calculating discriminant
discriminant = b**2 - 4*a*c
# 2. Check if the discriminant is positive, negative, or zero
if discriminant > 0:
# 3. If the discriminant is +ve, we will have two real roots
realRoot1 = (-b + math.sqrt(discriminant)) / (2*a)
realRoot2 = (-b - math.sqrt(discriminant)) / (2*a)
return ("This quadratic equation has two real roots:", realRoot1, realRoot2)
elif discriminant < 0:
# 3. If the discriminant is -ve, we will have two complex roots
real_part = -b / (2*a)
imaginary_part = math.sqrt(-discriminant) / (2*a)
compRoot1 = complex(real_part, imaginary_part)
compRoot2 = complex(real_part, -imaginary_part)
return ("This quadratic equation has two complex roots:", compRoot1, compRoot2)
else:
# 3. If the discriminant is 0, then we will have only one real root
realRoot = -b / (2*a)
return ("This quadratic equation has one real root:", realRoot)
# Sample values
a = 1; b = -5; c = 4
if a == 0:
print("Invalid equation! Please check the value of coefficient 'a' as it can not be 0.")
else:
result = find_DiscriminantAndRoots(a, b, c)
print(result)

Executing the code above will give us two real roots, 4 and 1. Let’s have a brief overview of the highlighted lines in the code widget above:

  • Line 1: We import the math library of Python.

  • Line 5: We calculate the discriminant.

  • Lines 8–12: We find the real roots if the value of the discriminant is positive.

  • Lines 14–20: We find the complex roots if the value of the discriminant is negative.

  • Lines 22–25: We find the real root if the value of the discriminant is neither positive nor negative.

  • Line 28: We define the sample coefficients for our quadratic equation.

  • Lines 30–31: We check the value of a to validate the quadratic equation.

  • Lines 32–34: If the equation is valid, then we call the find_DiscriminantAndRoots function and print the output.

Validate the roots

We can validate the roots by putting their values back in the original quadratic equation. Each root should satisfy the quadratic equation. Putting the values of the sample coefficients in the generic quadratic equation, we get the quadratic equation x25x+4=0x^2 - 5x + 4= 0.

Quiz

Let's test our understanding with a short quiz

1

What is the general form of a quadratic equation?

A)

ax2+bx+c=0ax² + bx + c = 0

B)

ax3+bx+c=0ax³ + bx + c = 0

C)

ax2+bx+d=0ax² + bx + d = 0

D)

ax2+b=0ax² + b = 0

Question 1 of 30 attempted

Conclusion

In this answer, we learned about the quadratic equation, its general form, and how to solve it using quadratic formula in Python. We saw a step-by-step implementation of the code and then validated the roots by putting them in the original equation. We saw that both roots satisfied the equation.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How do you solve the quadratic graph?

To solve a quadratic graph, find the x-intercepts (roots) by setting the quadratic equation equal to zero and solving for x, or find the vertex and axis of symmetry.


How to solve a parabola?

To solve a parabola, find its vertex, axis of symmetry, and roots (if any) using the quadratic formula or by completing the square.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved