Introduction to Flow Control
Many decisions need to be made in order to execute certain parts of your program. This is known as conditional execution or flow control. For conditional execution, we have to have something to compare to see if we need to execute or not. There are a group of comparison operators with the usual "greater than", "less than or equal to", "not equals", etc.
Comparison Operators
The comparison operators are as follows:
| Operator | Explanation | Usage |
|---|---|---|
| < | less-than | a < b |
| > | greater-than | a > b |
| <= | less-than, or equal-to | a <= b |
| >= | greater-than, or equal-to | a >= b |
| != | not-equal-to | a != b |
| == | equal-to | a == b |
Notice that equal-to requires two equal signs.
a = 10
b = 20
# This prints False
print(a > b)
# This prints True
print(a < b)
# This prints True
print(a == 10)
# This prints False
print(b != 20)
# This prints True
print(b >= 20)
If Statements
The primary method to controlling flow is to use an if statement. The if statement evaluates a condition. If that condition is True, the statement(s) are executed, otherwise, if the condition is False, the statement(s) are skipped.
The syntax for an if statement is:
if some_condition:
# If we get here, then some_condition evaluated to True
statement1
statement2
statement3
The statements 1, 2, and 3 belong to the if statement block by their indentation. Notice that the syntax is if, followed by the condition, and then a colon :. All statements that you want to be conditionally executed by this if statement must be indented on their own lines.
Else Statements
Sometimes, you want to make a choice: execute this OR that. Python has an else statement that only runs if the if statement evaluates to False.
if some_condition:
statement1
statement2
else:
# If we get here, the if statement evaluated to False
statement1
statement2
Elif Statements
Sometimes, you want to chain several conditions together. In this case, using the elif means else if. In other words, it means otherwise.
if a > 100:
# If we get here, a is greater than 100
elif a > 50:
# If we get here, a is greater than 50 and less than or equal to 100
elif a > 20:
# If we get here, a is greater than 20 and less than or equal to 50
else:
# If we get here, a is less than or equal to 20.
If Statement Example
a = 10
b = 20
if a > b:
print("A is greater than B")
The code above will only print A is greater than B if the variable a contains a value that is strictly greater-than the value containd in the variable b.
Mutual Exclusivity
Just like the rest of your Python program, if statements are computed from top to bottom. The following example does not function properly:
income = 35000.10
if income > 30000:
taxes = income * 0.25
if income > 20000:
taxes = income * 0.15
if income > 10000:
taxes = income * 0.10
else:
taxes = income * 0.02
With this code, it appears that we calculate the taxes based on a simple comparison. We have four tax brackets: $30,000, $20,000, $10,000, and everything below or equal to $10,000. In the code above, the only taxes that could ever be calculated are 10% or 2%. The other categories (25% and 15%) are going to be overwritten.
This is known as mutual exclusivity. Mutual exclusion is a case where two conditions cannot possibly be true at the same time. For example, let's say we have two integers: a and b. For any integer, it is not possible for a > b and a <= b at the same time. Therefore, a > b is exclusive to the condition a <= b. Mutual exclusivity allows us to write better code.
In other words, writing the following two if statements below is inefficient:
if a > b:
# We only run this code if the integer a is greater than the integer b.
print("A > B")
if a <= b:
# We only run this code if the integer a is less than or equal to the integer b.
print("A <= B")
With this code, you can see we make Python make two comparisons regardless of the values of a and b: if a > b and if a <= b. For example, if a is greater than b, we print A > B, but we also force Python to compare a <= b even though if a > b, it is impossible for a to be less than or equal to b. Always look for ways to save the CPU from having to make unnecessary calculations.
The code above can better be written as:
if a > b:
# We only run this code if the integer a is greater than the integer b.
print("A > B")
else:
# We only run this code if the integer a is less than or equal to the integer b.
print("A <= B")
Notice, with the comments I provided, both segments of code perform the exact same logical operations. However, by using mutual exclusivity, the CPU only has to make one comparison, not two.
Stephen Marz (2019)