Multi-line conditions in an
if statement in Python have been provided with various allowable ways in PEP8.
For starters, the multiple condition statements should not be placed in a single line. Instead, split this single line of the multiple conditions and wrap them in parentheses.
#do not define the multiple conditions in a single line like this if (firstcondition == 'something' and secondcondition == 'something else' and thirdcondition == something different): something_to_be_done
PEP8 guides on the use of continuation lines to separate the multi-line condition statements. These lines should be vertically aligned and spaced with a four-space indentation from the beginning of the new line. The closing parenthesis and the colon clearly show the end of the condition.
Apparently, Python has an automatic way of creating a four-space indentation whenever you combine a two-character keyword like
if, a single space, and an opening parenthesis. Therefore, there is no need to add extra indentation in the condition break.
if (this_matches_condition, this_also_matches_condition ):# end of conditions get_something_done()
If you style the multiple conditions like above, there is an advantage of easy readability of code. It also makes the complex conditions have a cleaner expression.
In a scenario where logic operator keywords like
or are included in the conditions, it is highly advisable, according to the PEP8 guide, to place the logic operator(and, or) before the continuation condition lines. This style is referred to as Knuth’s style. It enhances the readability of the conditions due to the automatic four-space indentation set by Python, to make all conditions vertically line up.
It also makes the conditions more visible within the parenthesis and brings along the advantage of short-circuit evaluation.
if ( sttmt1 and (sttmt2 or sttmt3) or name == 'Goodman' ): something_happens
An alternative way to style these multi-line condition statements is by defining a
variable and assigning these condition expressions to it. However, this method is not entirely encouraged because it tends to limit subsequent refactoring. However, the code will look clearer if a variable is used, and it also provides a better understanding of the program control flow.
condition_list= ( condition_one == 'number1' and condition_two == 'number2' and condition_three == 'number3' ) if conditions_list: implement_something else: something_else _is _done
The if statement in the above code ensures that something is implemented if and only if the entire
condition_list is true. Otherwise, something else is implemented when even one of the conditions in the
condition_list is false.
You can wrap an extra pair of parentheses around the conditions to visibly show the condition expressions.
if (( sttmt1 and (sttmt2 or sttmt3) or name == 'Goodman' )): something_happens
- Python Multiple if Statements on One Line
- Nested try...except Statements in Python
- if...else in One Line Python
- The for...else Statement in Python