Computer >> Computer tutorials >  >> Programming >> Python

How to Solve Quadratic Equation using Python?


You can use the cmath module in order to solve Quadratic Equation using Python. This is because roots of quadratic equations might be complex in nature. If you have a quadratic equation of the form ax^2 + bx + c = 0, then,

Example

import cmath

a = 12
b = 8
c = 1
# Discriminent
d = (b**2) - (4*a*c)
root1 = (-b - cmath.sqrt(d)) / (2 * a)
root2 = (-b + cmath.sqrt(d)) / (2 * a)
print(root1)
print(root2)

Output

This will give the output

(-0.5+0j)
(-0.16666666666666666+0j)