Python · Stage 3 – Making Decisions · Lesson 21 of 50

else and elif

Choose one path when a condition is false or test another condition.

is_raining = False
if is_raining:
    print("Take an umbrella")
else:
    print("Enjoy the sun")
Line-by-line explanation
  1. The first line stores False.
  2. The if condition checks that value.
  3. Its indented line is skipped.
  4. else provides the other path.
  5. The final message is displayed.
score = 70
if score >= 90:
    print("Gold")
elif score >= 60:
    print("Silver")
else:
    print("Bronze")
Line-by-line explanation
  1. The score is stored.
  2. The first condition is False.
  3. The elif condition is True, so Silver is displayed.
  4. Python skips the remaining else block.