So to start off a new area of discovery I have decided to start to learn Python.

To start off this undertaking I downloaded Python 2.7.3 from python/download and started to play around with IDLE, python’s Integrated DeveLopment Environment (IDE).

As a project to work on to learn this new language I decided to make a simple console application that try’s to calculates the value of X for a given quadratic. To do this it will use the quadratic formula

So to do this I first need the values of a, b, and c as per the formula.

for example a = 1, b = -9, and c = 20.

The first step logical step was to cut the equation into 3 chunks:

- The Discriminant (Δ); b^2 – 4ac
- sqrB = pow(b, 2)

AC = 4*a*c

Delta = math.sqrt(sqrB – AC) - the “pow(x,y)” function returns the first term to the power of the second, i.e. b^2
- the math.sqrt is a function of the math module which square roots the contents of the brackets, or to use the technical name, parentheses.

- sqrB = pow(b, 2)
- The Numerator; -b +- √Δ
- NumeratorPlus = -b + Delta

NumeratorMinus = -b – Delta

- NumeratorPlus = -b + Delta
- The Denominator; 2a
- Denominator = 2*a

Then with the necessary components the values of X can then be calculated by:

XPlus = NumeratorPlus / Denominator

XMinus = NumeratorMinus / Denominator

and then be outputed on the screen using the print command:

print (“Your answer is, X = “), XPlus, (“Or X = “), XMinus

notice the comma’s after each component which are needed.

And so the final step was to allow the user to enter in values:

print (“Please separate the quadratic equation into aX^2 + bX + C = 0”)

a=input(“Please Enter the Value of a : “)

b=input(“Please Enter the Value of b : “)

c=input(“Please Enter the Value of c : “)

The “input” function is important as it prints the given prompt to the output and then reads in the data entered by the user and assigns it to the variable.

This is different to the “raw_input” function which is not syntax sensitive.

And so the final code looks like:

import math

from time import sleep

print (“Please seperate the quadratic equation into aX^2 + bX + C = 0”)

a=input(“Please Enter the Value of a : “)

b=input(“Please Enter the Value of b : “)

c=input(“Please Enter the Value of c : “)

sqrB = pow(b, 2)

AC = 4*a*c

Delta = math.sqrt(sqrB – AC)

NumeratorPlus = -b + Delta

NumeratorMinus = -b – Delta

Denominator = 2*a

XPlus = NumeratorPlus / Denominator

XMinus = NumeratorMinus / Denominator

print (“Your answer is, X = “), XPlus, (“Or X = “), XMinus

sleep(10)