Conditionals (Flow Control)

Logical Expressions

An expression that evaluated to a Boolean value (True or False)

age = 21
age >= 21
True
age > 21
False
age > 35
False

If statements

We can use logical expressions in if-else statements:

if age > 15:
  allowed_to_drive = True
else:
  allowed_to_drive = False
  
print(allowed_to_drive)
True

When the expression age > 15 is evaluated to True lines 2 and 6 run, if it is evaluated to False line to is skipped over and lines 4 and 6 run.

If statements

Add elif statement if you need to check another condition:

if age >= 21:
  allowed_to_drink = True
  allowed_to_drive = True
elif age > 15:
  allowed_to_drink = False
  allowed_to_drive = True
else:
  allowed_to_drink = False
  allowed_to_drive = False
  
print(allowed_to_drink, allowed_to_drive)

When the expression age >= 21 is evaluated to True lines 2, 3 and 11 run, if it is evaluated to False lines 2-3 are skipped over and then age > 15 is evaluated. If age > 15 is evaluated to True lines 5, 6 and 11 run, if it is evaluated to False lines 5-6 are skipped over and lines 8, 9, and 11 run.

Short Project

Instructions for Short Project of the Week