print("TOP")
if 2 + 2 == 4:
print("THEY ARE EQUAL")
print("MORE STUFF")
print("BOTTOM")
TOP
THEY ARE EQUAL
MORE STUFF
BOTTOM
With conditional logic, the program will behave one way under certain conditions, and another way under different conditions.
The primary implementation of conditional logic in Python is the “if” statement.
You may be familiar with the “IF” function in spreadsheet software, but in Python we implement an “if” statement using some special syntax, like this:
TOP
THEY ARE EQUAL
MORE STUFF
BOTTOM
When we study “if” statements, we encounter the concept of “scope”. In Python, scope is represented by indentation level. Some indentation levels may only be reached under certain conditions, while some may never be reached at all.
print("TOP")
if 2 + 2 == 5:
print("THEY ARE EQUAL") # NEVER REACHED
print("MORE STUFF") # NEVER REACHED
print("BOTTOM")
TOP
BOTTOM
In this case, because the condition was not met, we never reach lines four and five.
Observe that the “else” clause is optional, and will represent a catch-all condition if the above conditions are not met.
print("TOP")
if 2 + 2 == 4:
print("THEY ARE EQUAL")
print("MORE STUFF")
else:
print("THEY ARE NOT EQUAL") # NEVER REACHED
print("BOTTOM")
TOP
THEY ARE EQUAL
MORE STUFF
BOTTOM
To implement multiple conditions, we can optionally include any number of “elif” clauses. They must come AFTER the “if”, and BEFORE the “else”:
print("TOP")
if 2 + 2 == 5:
print("EQUAL TO FIVE") # NEVER REACHED
elif 2 + 2 == 10:
print("EQUAL TO TEN") # NEVER REACHED
elif 2 + 2 == 4:
print("EQUAL TO FOUR")
else:
print("OOPS") # NEVER REACHED
print("BOTTOM")
TOP
EQUAL TO FOUR
BOTTOM
Observe that if more than one condition evaluates to being true, whichever is listed FIRST will be reached: