HOWTO · Python

Python Multi-Line if Condition

Python Multi-line if condition

On this page

To split a long if condition across lines in Python, wrap the expression in parentheses. Python then allows implicit line continuation, so you do not need a backslash at the end of each line. The continuation indentation is chosen for readability; Python does not automatically add four spaces for you. Keep the statement’s body indented beneath the if as usual.

Use Parentheses to Split a Long if Condition

Parentheses are the clearest default for a condition that does not fit comfortably on one line. They let you break the expression at meaningful points and keep the closing parenthesis beside the colon. Choose continuation indentation that makes the expression easy to scan, and use the same pattern consistently in a file.

Place Boolean Operators Before Continuation Lines

PEP 8 recommends breaking before a binary operator, which leaves and or or at the start of the next continuation line. This makes each additional check easy to spot. The placement is a formatting choice: parentheses and line breaks do not change how Python groups expressions or how and and or short-circuit. See the PEP 8 guidance on multiline if statements.

Here is a compact example with three checks. The body belongs to the if; the indentation inside the parentheses helps readers scan the expression.

plan = {"active": True, "paid": True, "seats": 4}

if (
    plan["active"]
    and plan["paid"]
    and plan["seats"] >= 3
):
    print("Eligible for the team plan.")
else:
    print("Choose another plan.")

With the values shown, all three checks are true, so the program prints:

Eligible for the team plan.

For mixed logic, use parentheses to make the intended grouping visible, such as (is_member or has_invitation) and account_is_active. If an earlier check should protect a later one, preserve that order: and and or evaluate from left to right with short-circuit behavior. Formatting alone does not make a different grouping equivalent.

Use Backslashes Only When Needed

A backslash can explicitly continue a statement, but it is more fragile than parentheses. The marker must be the final character on its physical line; trailing spaces or later edits after it can break the continuation. For example, a line may end with if account_is_active and \ before the next condition. Prefer parentheses for a long condition because they avoid this line-ending dependency and allow natural indentation. Reserve backslashes for cases where the surrounding syntax does not provide an implicit continuation context.

Name a Condition When It Improves Clarity

When a condition has a useful meaning of its own or is reused, assign it to a descriptive name such as eligible_for_team_plan, then write if eligible_for_team_plan:. This gives the if statement a concise, meaningful test and can make surrounding control flow easier to read. Keep the name consistent wherever it is assigned and used.

A named variable is a style choice, not a rule against refactoring. For a short expression used once, keeping the parenthesized condition beside the if may be clearer; for a longer or reused rule, a name may communicate intent better. Follow the conventions of the project and choose the form that makes the decision easiest for another developer to understand.