Python Code Snippets- Solving the Quadratic Equation

Want to explore the fun-learning ways of python programming to solve the quadratic equation? Then, this Python Code Snippets Solving Quadratic Equation Tutorial is the perfect one. Beginners and developers will find the two easy methods to solve the quadratic equation using python from this page. So, stay tuned to it and gain more knowledge about the concept of solving the quadratic equation using python with python programs.

The Tutorial of Python Code to Solve Quadratic Equation includes the following stuff:

Python Program to Solve Quadratic Equation

As we have said earlier that there are two methods to perform the python code snippets: solving the quadratic equation. The first method is using the direct formula and the second method is using the complex math module. Firstly, we will start discussing this concept by using the direct quadratic formula.

Standard Form of Quadratic Equations

ax² + bx + c
where,
a, b, and c are coefficient and real numbers and also a ≠ 0.
If a is equal to 0 that equation is not a valid quadratic equation.

The solution of this quadratic equation is as follows:

(-b ± (b ** 2 - 4 * a * c) ** 0.5) / 2 * a

Also Read: Quick Tip Using Pythons Comparison Operators

Method 1: Using the Direct Formula

The function written by our expertise solves the quadratic equation using basic multiplication and division operations in Python. If you don’t get it, to determine the quadratic equation you must take the opposite of b, plus or minus the square root of b squared, minus 4 times a times c over (divided by) 2 times a.

In the quadratic equation, a, b, and c can be any integers, positive or negative, as long as a doesn’t equal zero. In the example below, a equals 1, b equals 5, and c equals  6, but you can set them to be any numbers you like.

Python Code Snippet for Solving Quadratic Equation

Here is the snippet:

a = 1
b = 5
c = 6

# To take coefficient input from the users
# a = float(input('Enter a: '))
# b = float(input('Enter b: '))
# c = float(input('Enter c: '))

# calculate the discriminant
d = (b**2) - (4*a*c)

# find two solutions
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)

As you can see, the snippet obtains two possible solutions (as it’s meant to), because you’re meant to take the opposite of b plus or minus b squared, etc, and this equation accounts for that.

The snippet’s output is as follows:

print('The solution are {0} and {1}'.format(sol1,sol2))

Now that you have the formula, add your own numbers to see if the equation works for you.

Method 2: Using Complex Math Module

Here, we are importing the cmath module to solve and execute complex square root. At first, we calculate the discriminant and later find the two solutions of the quadratic equation. Have a look at the python code snippet below and solve the quadratic equation easily by importing cmath module:

Python Code to Solve Quadratic Equation using cmath module

# Solve the quadratic equation ax**2 + bx + c = 0

# import complex math module
import cmath

a = 1
b = 5
c = 6

# calculate the discriminant
d = (b**2) - (4*a*c)

# find two solutions
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)

print('The solution are {0} and {1}'.format(sol1,sol2))

Output: 

Enter a: 1
Enter b: 5
Enter c: 6
The solutions are (-3+0j) and (-2+0j)

Code Snippets: Using Python to Solve the Quadratic Equation

Python is a versatile and powerful coding language that can be used to execute all sorts of functionalities and processes. One of the best ways to get a feel for how Python works is to use it to create algorithms and solve equations. In this example, we’ll show you how to use Python to solve one of the more well-known mathematical equations: the quadratic equation (ax2 + bx + c = 0).

import cmath

print('Solve the quadratic equation: ax**2 + bx + c = 0')
a = float(input('Please enter a : '))
b = float(input('Please enter b : '))
c = float(input('Please enter c : '))
delta = (b**2) - (4*a*c)
solution1 = (-b-cmath.sqrt(delta))/(2*a)
solution2 = (-b+cmath.sqrt(delta))/(2*a)

print('The solutions are {0} and {1}'.format(solution1,solution2))

As you can see, in order to solve the equation the cmath module must be imported, and equation is solved by using multiplication, division, and the cmath.sqrt method (which can be used to find the square root of a number). The printed text can be customized to say anything you like.

Recommended Reading On: Python Program To Print Cube Number Series 1 8 27 64…N

Leave a Reply

Your email address will not be published. Required fields are marked *