Table of contents
1.
Introduction
2.
How do you do the Quadratic Equation in Python  
3.
Example
4.
Using the Quadratic Formula to Solve Quadratic Equations in Python
5.
Using the cmath Module to Solve Quadratic Equations in Python
5.1.
Python Program
6.
How do you find the Roots of a Quadratic Equation in Python  
6.1.
Example Run
7.
Frequently Asked Questions
7.1.
What happens if the discriminant is negative?
7.2.
Why do we use math.sqrt() in one method and cmath.sqrt() in another?
7.3.
Can I solve any quadratic equation with these methods?
8.
Conclusion
Last Updated: Aug 19, 2025
Easy

Python Program to Solve Quadratic Equation

Author Gaurav Gandhi
0 upvote

Introduction

A quadratic equation is a second-degree polynomial equation in the form ax² + bx + c = 0, where a, b, and c are constants. In  Python, we can solve this equation using built-in functions to handle real and complex roots efficiently. 

Python Program to Solve Quadratic Equation

This article will guide you through writing a Python program to find the roots of a quadratic equation, ensuring accuracy and efficiency.

How do you do the Quadratic Equation in Python  

To solve a quadratic equation in Python, we first need to understand the quadratic formula. The quadratic formula is used to find the roots of a quadratic equation of the form ax² + bx + c = 0. The formula is:  

Formula

Here, a, b, & c are the coefficients of the equation. The term under the square root, b² - 4ac, is called the discriminant. The discriminant determines the nature of the roots:  

  • If the discriminant is positive, there are two real & distinct roots.  
     
  • If it is zero, there is exactly one real root (repeated).  
     
  • If it is negative, the roots are complex & conjugate.  


Now, let’s write a  Python program to solve a quadratic equation. Below is the complete code from start to end:  


Import the math module to use the square root function

import math


Function to solve the quadratic equation

def solve_quadratic(a, b, c):
     Calculate the discriminant
    discriminant = b2 - 4ac
    
     Check the nature of the discriminant
    if discriminant > 0:
         Two real & distinct roots
        root1 = (-b + math.sqrt(discriminant)) / (2a)
        root2 = (-b - math.sqrt(discriminant)) / (2a)
        return root1, root2
    elif discriminant == 0:
         One real root (repeated)
        root = -b / (2a)
        return root,
    else:
         Complex roots
        real_part = -b / (2a)
        imaginary_part = math.sqrt(-discriminant) / (2a)
        return (real_part + imaginary_part1j), (real_part - imaginary_part1j)


Input coefficients from the user

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


Solve the quadratic equation

roots = solve_quadratic(a, b, c)


Display the roots

print("The roots are:", roots)


In this Code:  

1. Importing the Math Module: We use the `math` module to access the `sqrt()` function, which calculates the square root of the discriminant.  
 

2. Defining the Function: The `solve_quadratic()` function takes three arguments: `a`, `b`, & `c`, which are the coefficients of the quadratic equation.  
 

3. Calculating the Discriminant: The discriminant is calculated using the formula `b² - 4ac`.  
 

4. Checking the Discriminant:  

  • If the discriminant is positive, we calculate two real roots using the quadratic formula.  
     
  • If it is zero, we calculate one real root.  
     
  • If it is negative, we calculate complex roots using the real & imaginary parts.  
     

5. User Input: The program asks the user to input the values of `a`, `b`, & `c`.  
 

6. Displaying the Roots: The roots are printed based on the nature of the discriminant.  


This program is a complete implementation of solving a quadratic equation in Python. It handles all three cases: real & distinct roots, repeated roots, & complex roots.  

Example

A quadratic equation follows the standard form:

ax^2 + bx + c = 0

 

For example, if we take:

2x^2 + 3x - 2 = 0

 

Here, a = 2b = 3, and c = -2.

We can solve this equation in Python using different approaches.

Using the Quadratic Formula to Solve Quadratic Equations in Python

The quadratic formula is:

x = (-b ± √(b^2 - 4ac)) / 2a
Let's implement this in Python:
import math
def solve_quadratic(a, b, c):
    # Calculate the discriminant
    discriminant = b**2 - 4*a*c 
    # Check if roots are real or complex
    if discriminant >= 0:
        root1 = (-b + math.sqrt(discriminant)) / (2 * a)
        root2 = (-b - math.sqrt(discriminant)) / (2 * a)
        return root1, root2
    else:
        return "Complex Roots"

# Example usage
a = 2
b = 3
c = -2
roots = solve_quadratic(a, b, c)
print("The roots are:", roots)
You can also try this code with Online Python Compiler
Run Code

 

Output:

The roots are: (0.5, -2.0)

 

Explanation:

  1. We calculate the discriminant using b² - 4ac.
     
  2. If the discriminant is non-negative, we compute the two real roots using the quadratic formula.
     
  3. If the discriminant is negative, the roots are complex, and this method does not handle them.

Using the cmath Module to Solve Quadratic Equations in Python

For handling complex roots, we use Python’s cmath module, which allows calculations involving complex numbers.

Python Program

import cmath

def solve_quadratic_cmath(a, b, c):
    # Calculate the discriminant
    discriminant = cmath.sqrt(b**2 - 4*a*c)
    
    # Compute the roots
    root1 = (-b + discriminant) / (2 * a)
    root2 = (-b - discriminant) / (2 * a)
    return root1, root2

# Example usage
a = 1
b = 2
c = 5
roots = solve_quadratic_cmath(a, b, c)
print("The roots are:", roots)
You can also try this code with Online Python Compiler
Run Code

 

Output:

The roots are: ((-1+2j), (-1-2j))

 

Explanation:

  1. The cmath.sqrt() function is used to compute the square root of the discriminant even when it is negative.
     
  2. This allows us to handle both real and complex solutions.

How do you find the Roots of a Quadratic Equation in Python  

Finding the roots of a quadratic equation is the process of solving for the values of x that satisfy the equation ax² + bx + c = 0. In Python, we can achieve this by implementing the quadratic formula step by step. Let’s understand the implementation & write a program to find the roots.  

Below is the complete Python code to find the roots of a quadratic equation:  


Import the math module for square root calculation

import math


Function to find the roots of a quadratic equation

def find_roots(a, b, c):
     Calculate the discriminant
    discriminant = b2 - 4ac
    
     Check the nature of the discriminant
    if discriminant > 0:
         Two real & distinct roots
        root1 = (-b + math.sqrt(discriminant)) / (2a)
        root2 = (-b - math.sqrt(discriminant)) / (2a)
        return root1, root2
    elif discriminant == 0:
         One real root (repeated)
        root = -b / (2a)
        return root,
    else:
         Complex roots
        real_part = -b / (2a)
        imaginary_part = math.sqrt(-discriminant) / (2a)
        return (real_part + imaginary_part1j), (real_part - imaginary_part1j)


Input coefficients from the user

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


Find the roots

roots = find_roots(a, b, c)


Display the roots

print("The roots of the quadratic equation are:", roots)


In the Code:  

1. Importing the Math Module: We use the `math` module to access the `sqrt()` function, which is necessary for calculating the square root of the discriminant.  
 

2. Defining the Function: The `find_roots()` function takes three parameters: `a`, `b`, & `c`, which are the coefficients of the quadratic equation.  
 

3. Calculating the Discriminant: The discriminant is calculated using the formula `b² - 4ac`. This value determines the nature of the roots.  
 

4. Checking the Discriminant:  
 

  • If the discriminant is positive, the equation has two real & distinct roots. These roots are calculated using the quadratic formula.  
     
  • If the discriminant is zero, the equation has exactly one real root (repeated). This root is calculated using the formula `-b / (2a)`.  
     
  • If the discriminant is negative, the equation has two complex roots. These roots are calculated using the real & imaginary parts.  
     

5. User Input: The program prompts the user to input the values of `a`, `b`, & `c`.  
 

6. Displaying the Roots: The roots are printed based on the nature of the discriminant.  

Example Run

Let’s say we have the quadratic equation 2x² + 5x + 3 = 0. Here, `a = 2`, `b = 5`, & `c = 3`.  

When we run the program:  

Enter coefficient a: 2  
Enter coefficient b: 5  
Enter coefficient c: 3  


The output will be:  

The roots of the quadratic equation are: (-1.0, -1.5)


This means the roots are x = -1.0 & x = -1.5.  


This program is a practical implementation of finding the roots of a quadratic equation in  Python. It works for all types of roots: real, repeated, & complex.  

Frequently Asked Questions

What happens if the discriminant is negative?

If the discriminant is negative, the equation has complex roots. In such cases, we must use the cmath module to compute the solutions correctly.

Why do we use math.sqrt() in one method and cmath.sqrt() in another?

math.sqrt() is used when we expect real roots, whereas cmath.sqrt() is used when we need to handle complex roots as well.

Can I solve any quadratic equation with these methods?

Yes! These methods work for all quadratic equations, regardless of whether the roots are real or complex.

Conclusion

In this article, we learned how to write a Python program to solve a quadratic equation using the quadratic formula. By taking coefficients as input, we calculated the roots of the equation using the discriminant method. Understanding this concept helps in mathematical computations, algorithm development, and scientific programming in  Python.

Live masterclass