Hi.
If you want to
solve quadratic equation with given coefficients (numbers) then you should find (calculate) numbers x such that a*x**2+b*x+c == 0. That means you should define a function which performs such calculation, e.g.:
Code:
from cmath import sqrt # numerical math functions in complex domain
# solves equation
# a*x^2 + b*x + c = 0
# returns: [D, x1, x2],
# where D (discriminant) could be:
# positive - two different real roots;
# 0 - x1=x2, real;
# negative - both roots are complex.
def solve_quadratic(a,b,c):
D = b**2 - 4*a*c
x1 = (-b + sqrt(D) )/2.0/a
x2 = (-b - sqrt(D) )/2.0/a
return [D, x1, x2]
# test
print solve_quadratic(1,4,1)
If you want to save a quadratic expression to a variable symbolically then you should probably look at `sympy', `sage', `maxima' (not in python) or similar software for symbolic mathematics.
Finally, if you want to be able to compute quadratic expression a*x**2+b*x+c with different values of `x' you may define a function, for example:
Code:
# values of a,b and c are global
quadratic_equation = lambda(x): a*x**2+b*x+c
# evaluate at x=5
print quadratic_equation(5)