next up previous
Next: Exiting the Code Up: The Code Previous: Python code

Explanation of the Program

Here we explain some new features in the code

import math

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.

# Compute the real roots of the quadratic equation

Comments are ignored by the compiler. They start with #. A liberal use of comments is good programming practice. It helps you and others understand what your code is doing.

a, b, c = eval(input("Enter a, b, c "))

Here we are inputing a list of three values at once. The user must type them with commas between them.

  d = b**2 - 4*a*c

In Python multiplication is expressed with a * and a power, with **.

  d = b**2 - 4*a*c
  signb = 1

These are assignment statements and not simply equations. In an assignment statement a single variable name appears on the left and an expression appears on the right. The statement instructs the computer to evaluate the expression on the right and assign the result to the variable on the left. So the equal sign implies movement to the left.

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 += y
is an abbreviation for
  x = x + y
Other compound assignments are -=, *=, and /=.

  if b < 0:
       signb = -1

This is a simple if statement. We are using it to make signb be $+1$ if b is positive and $-1$ if negative. The logic of this code is first to assign $+1$ and then correct it if b is negative.

The formal syntax for a simple if statement is

  if logical-expression:
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 $b$ 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 $a$ and $b$ 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 $b$ with a simple if else pair:

  if b < 0:
      signb = -1
      signb = 1
In 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.")
    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.

 s = -(b + math.sqrt(d)*signb)/2.

Here we use the math package 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 $2a$, 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)/a
gives 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.

  x1 = s/a
  x2 = c/s

  print("Roots are", x1, " ", x2)

Notice that one root is computed with one solution formula and the other with the other, as promised.

next up previous
Next: Exiting the Code Up: The Code Previous: Python code
Carleton DeTar 2017-09-05