Control Structures has a comprehensive discussion on control flow and structures concepts in programming. This page deals specifically with control structures within Python.
Conditionals take an expression, which is code that evaluates to determine a value, and checks if it is True or False. If it’s True, we can tell our program to do one thing — we can even account for False to do another. As we write more complex programs, conditionals allow us to address multiple scenarios and make our programs more robust.
The Python if
statement is used to determine the execution of code based on the evaluation of a Boolean expression.
if
statement expression evaluates to True
, then the indented code following the statement is executed.False
then the indented code following the if statement is skipped and the program executes the next line of code which is indented at the same level as the if statement.Boolean expressions can only be True
or False
today = “Monday” # a string representing the day of the week
is_weekend = False # a boolean expression to determine if its the weekend
Relational operators compare two items and return either True
or False
. For this reason, you will sometimes hear them called comparators.