Book
Module 2 - Section 1 - Controlling the flow
Module 2 - Section 1 - Controlling the flow
Completion requirements
View
After the completion of the this module the students will be able to:
- create code using the if statement
- recognize and use indented blocks of code
- solve Boolean algebra logic problems
- create code using the complicated if statements with elif and else
- recognize nested if statements and use them
2.1.3 - And and Or
An if statement can check multiple conditions by chaining together comparisons with and and or. These are also considered to be operators just like + or - are.
# And
if a < b and a < c:
print("a is less than b and c")
# Non-exclusive or
if a < b or a < c:
print("a is less than either b or c (or both)")
A common mistake is to omit a variable when checking it against multiple conditions. The code below does not work because the computer does not know what to check against the variable c. It will not assume to check it against a.
# This is not correct
if a < b or < c:
print("a is less than b and c")