Solving the quadratic equation

Here, we will try to solve the quadratic equation $ax^2+bx+c$.

Powers and roots

Here is how powers and square roots are computed in Python:

In [1]:
5**2
Out[1]:
25

Note that you should use the ** operator. ^ does something different:

In [2]:
5^2
Out[2]:
7

We can of course use $x^{0.5}$ to compute square roots:

In [3]:
64**0.5
Out[3]:
8.0

We can also use the following:

In [4]:
import math    #has to be in the code before we use math.sqrt
math.sqrt(225)
Out[4]:
15.0

Now, we are ready to begin writing the code:

In [5]:
import math

a = 1   
b = 5 
c = 2
disc = b**2-4*a*c

if disc > 0:
    r1 = (-b + math.sqrt(disc))/(2*a)
    r2 = (-b - math.sqrt(disc))/(2*a)
elif disc == 0:
    r = (-b + math.sqrt(disc))/(2*a)
else:
    print("There are no real solutions")