Here we explain some new features in the code quadratic.py
.

This import
statement causes Python to use the math
package with your program. The math package has many special
functions. The one we need here is sqrt
.



*
and a power,
with **
.

Note: All variables in an expression on the right hand side must have
previously been given assigned values. Here the variables a
, b
, and c
are assigned values in
the previous input()
statement. It is a common mistake
to forget to assign values to variables before using them. If you
make this mistake, you might get very strange results.
In addition to simple assignments, Python has compound assignment statements. For example
x += yis an abbreviation for
x = x + yOther compound assignments are
=
, *=
, and
/=
.

signb
be if b
is positive and if
negative. The logic of this code is first to assign and then
correct it if b
is negative.
The formal syntax for a simple if statement is
if logicalexpression: statement statement ...Notice the required colon following the logical expression. The statements controled by the if statement must be indented. With emacs editing, simply use the <Tab> key to indent.
A logical expression is one that evaluates to true or false. A
nonzero numeric value is considered to be true, and a zero value,
false. If the expression is true, the indented statement or
statements are executed. Otherwise it is skipped. Here we are
checking whether is less than zero. We use the relational "is
less than" <
operator. Here is a list of relational
operators:
is less than  
is greater than  
is less than or equal to  
is greater than or equal to  
is equal to  
is not equal to 
Logical expressions can be combined with parentheses and logical operators to form more complex logical expressions. So to test whether and are both zero we do
if a == 0 and b == 0: print("Yes, a and b are both zero.")The logical operators are simply
and 
AND 
or 
OR 
not 
NOT 
Returning to the code for solving a quadratic, we could have also determined the sign of with a simple if else pair:
if b < 0: signb = 1 else: signb = 1In this case the statement following
else
is executed if the
logical expression is false.
Please pay careful attention to the indenting here. The else
that pairs with the if
must have the same level of indentation.
The statements that they control must be indented further.
We can have have several statements controlled by if
and
else
:
if b < 0: signb = 1 print("b is negative.") else: signb = 1 print("b is nonnegative.") }In this case we have a block of statements to execute if the expression is true and a block to execute if it is false. We must indent the statement block as a whole.

math.sqrt
function. We
indicate that we want to use the sqrt
function that is found in
the math
package by putting the package name in front followed
by a dot .
The syntax for the computed expression corresponds to what one expects
algebraically: Take the square root of d
and multiply the
result by signb
before adding to b
. The
outer parentheses force the addition to happen before dividing by 2
and reversing the sign.
There are a couple of subtleties about the order of calculation that often trip up beginners. The following expression gives an incorrect result for the root of the quadratic:
x1 = (b + math.sqrt(d)*signb)/2*a # WRONG!Here division by 2 happens first and the result is then multiplied by
a
, effectively putting a
in the numerator.
To divide by the product , it is necessary to use parentheses:
x1 = (b + sqrt(d)*signb)/(2*a) # correct
A word about Python 3 integer division for students familiar with C, C++, or Python2: In those languages integer division keeps the quotient and discards the remainder. This is sometimes called "floor division". So, for example, 1/2 evaluates to integer zero. So the statement
x1 = (1/2)*(b + math.sqrt(d)*signb)/agives zero in Python2  not what we want! Python3 changes the convention, so 1/2 evaluates to floating 0.5, which is certainly what we intended in this case. Integer floor division does have its uses in coding, so Python3 introduces a double slash
//
to specify
it, as in 1//2
.
