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.

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:

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.



