A Boolean expression with multiple conditions can get quite long. How to keep things concise?


#1

Question

A Boolean expression with multiple conditions can get quite long. How can we keep things concise?

Answer

If you have a long expression with multiple expressions combined with and and or, what you can do is “nest” other if/elif/else statements.

This can be helpful when you want to continue checking other conditions given that some previous condition is met. If the previous condition is not met, then it would not check any further into the nested statements.

Example

# if statement with nested if/else statement
if time == 8:
  if weekend:
    snooze()
  else:
    wakeup()