We introduce if/else statements in Python.

If/Else

In an earlier section we saw that in order to compute the absolute value of a real number , we first needed to determine if it satisfied a certain condition, namely, is it the case that ? The answer to this question then determines the set of instructions to follow. We can reformulate our algorithm for computing as two statements in the following ways:

For any real number , if , then . Else (or otherwise), .

The ‘if’ portion of the statement explains what to do if the given condition is satisfied, while the ‘else’ portion explains what to do if the given condition is not satisfied.

We can express the same idea as above in pseudocode below. (Note that pseudocode will look a lot like Python syntax, but they are not the same.)

==============================
 
procedure absolute(x: real number)  
        IF x > 0, THEN absVal = x  
        ELSE absVal = -x  
        RETURN absVal (the absoluve value of x)  
==============================

The block of pseudocode above has several important features:

  • The word absolute is the name of this algorithm.
  • The x in parentheses is the input.
  • The IF, THEN, ELSE, etc. represent keywords in the individual steps taken in the algorithm. (For a more complete treatment of this, see Rosen’s Discrete Mathematics and Its Applications textbook.)
  • The term absVal is a variable used to compute the absolute value of x.
  • The variable after the RETURN keyword is what the procedure outputs.

These two new ways of expressing an algorithm can be easily mapped to the flowchart for computing below.

PIC

In Python we can implement this short algorithm using the following syntax:

==============================
 
if condition:  
        code1  
else:  
        code2  
==============================

The term condition indicates the condition we wish to check. If the condition evaluates to True, then the instructions given by code1 are followed. If the condition evaluates to False, then the instructions given by code2 are followed. Using the template above, we can now write the necessary Python code to compute .

==============================
 
if x > 0:  
        absVal = x  
else:  
        absVal = -x  
==============================

The code above creates a variable absVal that is assigned the absolute value of for any given . The SageCell below shows how to use this code in practice. We assign the desired output to a variable so that we can use it later for whatever we want.

Note that in Python, whitespace (indentation) determines which lines of code belong to a particular code block. For example, the following code differs from that in the SageCell above in that the print statement is only called if .

The example above shows how a flowchart with a decision compartment can be translated to Python code and vice versa. With enough practice one shoud be able to go between the two. Eventually, with enough experience, one can start coding without explicitly drawing a flowchart each time.

Elif

Below we give examples of how to expand on the basic if/else form to handle cases where multiple decisions are needed or if a decision has more than two outcomes.

The flowchart below shows an algorithm for computing the function seen in the Flowchart problem section.

PIC

This can be implemented in Python in several ways.

We can put an if/else statement inside of another if or else block of code. Note the indentation of the lines belonging to the second if/else statement.

We can use the elif (else-if) option that simply shortens the code above.

==============================
 
if condition1:  
        code1  
elif condition2:  
        code2  
elif condition3:  
        code3  
    ...  
elif conditionk:  
        codek  
else:  
        code(k+1)  
==============================

The elif lines above allow one to write a program that checks multiple conditions. The program will execute whatever instructions are associated with the first (and only the first) condition checked that is satisfied. If none of the conditions are satisfied, then the default behavior is given by the code in the else portion.

Note that the second option greatly increases the readability of the code and can be expanded with more elif lines if more options must be considered.

Finally, a condition checked by an if/elif statement need not involve a comparison operator. Any nonzero number is treated as True, while zero is treated as False. See the example below.

Problems

Consider using the SageCell below as your workspace for answering the following questions.

Define the Heaviside step function for any real number as Develop an algorithm to implement the Heaviside function. Express your algorithm as a flowchart and in Python. In Python, assign the desired output to a variable H.
This problem is similar to computing . Note that the second hint for this problem is the solution.

PIC

A different definition of the Heaviside step function gives a different value at , namely, Develop an algorithm to implement this alternative Heaviside function. Express your algorithm as a flowchart and in Python. In Python, assign the desired output to a variable H.
This problem is similar to computing . Note that the second hint for this problem is the solution.

PIC

Express your algorithm solution from the first question in the Flowcharts with Operators section in Python. In Python, assign the desired output to a variable real_roots. Note that the hint for this problem is the solution.
Develop an algorithm that takes in three real numbers , , and with and determines the number of distict real roots of the quadratic polynomial . The output should be the the number of distinct real roots. Express your algorithm as a flowchart and in Python. In Python, assign the desired output to a variable real_roots_count.
As in the previous problem, the solution is dependent on the value of the quantity . Note that the second hint for this problem is the solution.

PIC

Suppose you are given three distinct real numbers , , and . Develop an algorithm that determines which one is the largest. Express your algorithm as a flowchart and in Python. In Python, assign the desired output to the variable maximum. Note that the hint for this problem is the solution.

PIC

Workspace