The Easiest Way to Solve Quadratic Equation using Python

In this Python example, we will discuss how can we solve any mathematical quadratic equation. Let’s get started.

1. What is Quadratic Equation and How to solve it?

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, b are the coefficients, x is the variable, and c is the constant term.

According to Cuemath

The word “Quadratic” is derived from the word “Quad” which means square. Simply, a quadratic equation is an “equation with degree 2“.

In this program, we will use the given coefficients a, b and c to calculate the roots of a quadratic equation. a, b, and c are real numbers.

EXAMPLE:

Input : a = 4, b = 2, c = 5  
Output : Real and Different Roots
1.3333333333333333
1.0

Input : a = 4, b = 4, c = 3 
Output : Complex Roots 
-0.5  + i 5.656854249492381
-0.5  - i 5.656854249492381

Some of the topics which will be helpful for understanding the program implementation better are:


2. Solving Quadratic Equation using Formula

As discussed above, the quadratic equation intakes three parameters. For solving the quadratic equation, we can directly apply the formula to find the roots.

The formula to find the roots of the quadratic equation is:

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

Some of the important points which should be followed for solving the quadratic equations are:

  • The form ax2 + bx + c = 0 will be followed, as this is the standard form.
  • The discriminant of the quadratic equation is D = b– 4ac
  • If D > 0, then the roots are real and distinct.
  • If D = 0, then the roots are real and equal.
  • If D < 0, then the roots do not exist, or the roots are imaginary.
  • The quadratic equation having roots α, β, is x2 – (α + β)x + αβ = 0.

For this program, we will use the math library, we will use the command import math.

Let’s implement the code and see how it works.

#Python program to solve quadratic equation using formula
import math 
  
# Finding the roots using the Function
def roots_of_equation(a, b, c): 
  
  # Finding the value of Discriminant 
  D = b*b - 4*a*c 
  # other way, D = b**2 - 4*a*c
    
  sqrt_D = math.sqrt(abs(D)) 
      
  # checking Discriminant condition 
  if D > 0: 
    print("Roots are Real and Different ") 
    print((-b + sqrt_D)/(2*a)) 
    print((-b - sqrt_D)/(2*a)) 
      
  elif D == 0: 
    print(" real and same roots") 
    print(-b / (2*a)) 
      
  # Discriminant < 0 follows else block
  
  else:
    print("Complex Roots") 
    print(- b / (2*a), " + i", sqrt_D) 
    print(- b / (2*a), " - i", sqrt_D) 
    
    
  
# MAIN
a = 4
b = 4
c = 3

# We can ask the user for value for a, b, c
  
# If a is given 0, then Equation is incorrect
if a == 0: 
  print("Input correct Quadratic Equation")   
else:
  roots_of_equation(a, b, c)
Output
Complex Roots
-0.5  + i 5.656854249492381
-0.5  - i 5.656854249492381

In the above program, we have set the value of a, b, and c, instead, we can also take the value from the user.

The function roots_of_equation has been defined to calculate the Discriminant value and to check the conditions after the Discriminant value has been calculated. Accordingly the output will be printed.

If you want to know more about the math module, you can click here.


3. Solving Quadratic Equation using the Complex math Module

As we know, Python is filled with different libraries, functions, and modules. Hence, we are now going to use one of the powerful modules which is cmath.

#Python program to solve quadratic equation using math module
import cmath

print("The Quadratic Equation is of form ax2 + bx+ c\n")  
a = int(input("Enter the value of a: "))
b = int(input("Enter the value of b: "))
c = int(input("Enter the value of c: "))
  
# Computing the Discriminat value
D = (b**2) - (4*a*c)
  
# find two results
root1 = (-b - cmath.sqrt(D))/(2 * a)
root2 = (-b + cmath.sqrt(D))/(2 * a)
  
# printing the results
print("The roots are: ")
print(root1)
print(root2)
Output
The Quadratic Equation is of form ax2 + bx+ c

Enter the value of a: 4
Enter the value of b: 7
Enter the value of c: 3
The roots are: 
(-1+0j)
(-0.75+0j) 

In the program, we have taken input from the user for all the three variables a, b, and c. The program directly tells the root since there is no particular condition we need to define.

We have used the cmath.sqrt(), cmath is actually a header file that contains different functions including the square root. This module provides access to mathematical functions for complex numbers. The functions in this module accept integers, floating-point numbers, or complex numbers as arguments.

If you want to know more about cmath module, then you may click here.

4. Conclusion

In this article, we have learned how to solve any quadratic equation using Python program implementation. We have implemented and discussed both the important ways.


Helpful Links

Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.

Also for examples in Python and practice please refer to Python Examples.

Complete code samples are present on Github project.

Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! 😊

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x