Search code examples
pythonif-statementnestedlogic

When would nested Ifs be more useful than If-ands in Python logic?


When do we use nested Ifs and If-ands?

For example, if I wanted to write a code:

If (Income less than cut off): If (Canadian Citizen?): Receive social assistance

When would I use nested IF like above instead of If-and? At first I assumed it was when we want to access the inner statement only when it evaluates to true but with IF-and you can still skip the inner statement if it evaluated to false.


Solution

  • if cond_1 is True and cond_2 is True:
      do_this()
    ...
    

    and

    if cond_1 is True:
      if cond_2 is True:
        do_this()
      else:  # cond_2 is False but cond_1 is still True
        do_other()
    ...