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.

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.

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.



