How to Find the Roots of a Quadratic Equation in Python

In this tutorial, we will learn how to program "How to Find the Roots of a Quadratic Equation in Python." The objective is to calculate the roots of a quadratic equation using a step-by-step approach. A quadratic equation takes the form ax² + bx + c = 0, and the roots can be found using the quadratic formula. This tutorial will walk you through implementing this in Python, using the discriminant to determine the nature of the roots—real and distinct, real and equal, or complex. By the end of this tutorial, you will have a clear understanding of how to handle and compute the roots of any quadratic equation efficiently in Python.

This topic is straightforward to understand. Just follow the instructions I provide, and you will complete it with ease. The program I will demonstrate will show you the correct and efficient way to swap the first and last characters of a string. So, let’s dive into the coding process!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. while True:
  2.     print("\n============== Find the Roots of a Quadratic Equation ==============\n")
  3.  
  4.     print("Equation: ax^2 + bx + c ")
  5.     a=int(input("Enter a: "))
  6.     b=int(input("Enter b: "))
  7.     c=int(input("Enter c: "))
  8.     d=b**2-4*a*c
  9.     d1=d**0.5
  10.     if(d<0):
  11.         print("The roots are imaginary. ")
  12.     else:
  13.         r1=(-b+d1)/2*a
  14.         r2=(-b-d1)/2*a
  15.         print("The first root: ",round(r1,2))
  16.         print("The second root: ",round(r2,2))
  17.  
  18.     opt = input("\nDo you want to try again?(yes/no): ")
  19.            
  20.     if opt.lower() == 'yes':
  21.         ret=False
  22.     elif opt.lower() == 'no':
  23.         ret=True
  24.         print("Exiting program....")
  25.     else:
  26.         print("Please enter yes/no:")
  27.         break
  28.  
  29.     if ret == False:
  30.         continue

This Python program calculates the roots of a quadratic equation in the form ax² + bx + c. It prompts the user for coefficients a, b, and c, then computes the discriminant to determine the nature of the roots. If the discriminant is negative, the program informs the user that the roots are imaginary. Otherwise, it calculates and displays the two real roots. The program loops, allowing repeated use until the user chooses to exit.

Output:

There you have it we successfully created How to Find the Roots of a Quadratic Equation in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment