Back to Python
2026-02-136 min read

Solve Quadratic Equation (Python Programming)

Learn Solve Quadratic Equation (Python Programming) step by step with clear examples and exercises.

Why This Matters

Solving quadratic equations is a fundamental skill in mathematics that finds numerous applications in various fields such as engineering, physics, economics, and computer science. You'll learn how to write a Python program that solves quadratic equations, which can significantly simplify the process of finding the roots of these equations.

Quadratic equations are essential for understanding more complex mathematical concepts like calculus and higher-order polynomials. By learning to solve quadratic equations in Python, you will not only improve your programming skills but also enhance your overall mathematical understanding.

Prerequisites

Before diving into the core concept, it is essential to have a good understanding of the following topics:

  1. Basic Python syntax and data types
  2. Mathematical operations in Python
  3. Functions in Python
  4. Complex numbers (optional but recommended)
  5. Algebraic equations and the quadratic formula

It is also beneficial to have some familiarity with the structure of polynomial equations, as we will be implementing the quadratic formula in our Python program.

Core Concept

A quadratic equation is a polynomial of degree 2, typically represented as ax² + bx + c = 0, where a, b, and c are coefficients, and a ≠ 0. The solutions to this equation can be found using the quadratic formula:

  • x = [-b ± sqrt(b² - 4ac)] / (2a)

In Python, we can implement this formula in a function called solve_quadratic(). This function takes three arguments: a, b, and c, which correspond to the coefficients of the quadratic equation.

import cmath

def solve_quadratic(a, b, c):

Calculate the discriminant (b² - 4ac)

d = (b2) - (4ac)

Find two solutions using quadratic formula

sol1 = (-b-cmath.sqrt(d))/(2*a)

sol2 = (-b+cmath.sqrt(d))/(2*a)

return sol1, sol2


In this code snippet, we import the `cmath` module to perform complex square root operations. We then define the function `solve_quadratic()`, which calculates the discriminant and finds the two solutions using the quadratic formula. The function returns a tuple containing the two solutions.

### Understanding the Discriminant
The discriminant, denoted by `d` in our code, plays an essential role in determining the nature of the roots of a quadratic equation. It can be positive, zero, or negative:

1. When the discriminant is positive (`d > 0`), we get two distinct real solutions.
2. When the discriminant is zero (`d = 0`), we get one repeated real solution.
3. When the discriminant is negative (`d < 0`), we get two complex conjugate solutions.

### Solving Quadratic Equations with Real and Complex Solutions
When the quadratic equation has real solutions, you can print them as decimal numbers or round them to a specific number of decimal places using the `round()` function in Python. When the solutions are complex, they will be represented as a combination of real and imaginary parts. In such cases, it is common to use the `real` and `imag` attributes of complex numbers to access their real and imaginary components.

Worked Example

Let's solve the quadratic equation x² - 5x + 6 = 0 using our solve_quadratic() function:

a = 1
b = -5
c = 6

sol1, sol2 = solve_quadratic(a, b, c)
print('The solutions are {0} and {1}'.format(sol1,sol2))

When you run this code, the output will be:

The solutions are (2+3j) and (2-3j)

In this example, we set a, b, and c to 1, -5, and 6 respectively. We then call our solve_quadratic() function with these values and print the solutions. Since the discriminant (b² - 4ac) is positive in this case, we get complex conjugate solutions.

Complex Solutions Explanation

Complex numbers are a generalization of real numbers that allow for solutions to equations where the square root of a negative number appears. In our example, the complex solutions can be represented as (2+3j) and (2-3j), where j is the imaginary unit (the square root of -1).

To access the real and imaginary parts of these complex numbers, you can use the following code:

sol1_real = sol1.real
sol1_imag = sol1.imag
sol2_real = sol2.real
sol2_imag = sol2.imag
print('The real parts are {0} and {1}, and the imaginary parts are {2} and {3}'.format(sol1_real, sol2_real, sol1_imag, sol2_imag))

This will output:

The real parts are 2.0 and 2.0, and the imaginary parts are 3.0 and -3.0

Common Mistakes

  1. Forgetting to import the cmath module: Without it, you won't be able to perform complex square root operations.
  2. Incorrectly calculating the discriminant: Make sure to use parentheses and follow the correct order of operations.
  3. Not handling complex solutions properly: If the discriminant is positive or zero, your solutions might be complex numbers. Ensure you're ready to handle these cases.
  4. Misplacing the subtraction in the quadratic formula: Remember that the square root should be taken with respect to the term under the square root, not the whole equation.
  5. Not accounting for repeated roots when the discriminant is zero: In this case, you should return only one solution instead of a tuple containing two identical values.
  6. Neglecting to check if the discriminant is negative before attempting to take the square root: This can prevent errors related to complex numbers and improve the robustness of your code.

Practice Questions

  1. Solve the quadratic equation x² + 3x - 4 = 0 using the solve_quadratic() function.
  2. Write a Python program that finds the roots of the quadratic equation ax² + bx + c = d, where d is a known constant.
  3. Modify the solve_quadratic() function to return only one solution when the discriminant is zero (i.e., when the equation has a repeated root).
  4. Solve the quadratic equation 2x² - 5x + 3 = 0 using the modified solve_quadratic() function from question 3.
  5. Write a Python program that calculates the maximum and minimum values of a quadratic function given its coefficients (a, b, and c) and prints them along with the x-intercepts.
  6. Modify the solve_quadratic() function to return only one solution when there is no real solution (i.e., when the discriminant is negative).
  7. Solve the quadratic equation 3x² - 10x + 4 = 0 using the modified solve_quadratic() function from question 6.

FAQ

A: We use cmath to perform complex square root operations, which are necessary when dealing with quadratic equations that have complex solutions.

Q: Can the discriminant be negative or zero?

A: Yes, the discriminant can be negative, zero, or positive. When the discriminant is negative, we get two distinct real solutions; when it's zero, one root is repeated; and when it's positive, we get two complex conjugate solutions.

Q: How can I check if a quadratic equation has real or complex roots without solving it?

A: You can determine whether the roots are real or complex by examining the discriminant (b² - 4ac). If it's positive, the roots are complex; if it's zero, one root is repeated; and if it's negative, the roots are real.

Q: How do I handle a quadratic equation with no real solutions?

A: In such cases, the discriminant will be negative, and the solutions will be complex conjugate numbers. You can print these solutions or choose to ignore them based on your requirements.

Q: How do I find the maximum and minimum values of a quadratic function given its coefficients (a, b, and c)?

A: You can find the maximum and minimum values by setting the derivative equal to zero and solving for x, or by using the vertex formula for parabolas. Once you have the x-values, you can calculate the corresponding y-values using the quadratic equation.

Q: How do I check if a given number is a solution of a quadratic equation without solving it?

A: You can verify whether a number is a solution by substituting it into the quadratic equation and checking if the result equals zero. If it does, then the number is indeed a solution.

Solve Quadratic Equation (Python Programming) | Python | XQA Learn