HOWTO · Python

Python Multiple if Statements on One Line

Learn which Python conditional forms fit on one line, how conditional expressions work, and when to use a readable if/elif/else block.

On this page

Python can put several simple statements on one physical line when they are separated by semicolons, but an if statement is a compound statement and cannot be chained after a semicolon as another statement. For multiple branches, use a normal if/elif/else block. A conditional expression can choose one value on one line, while any() answers a different question: whether at least one item in an iterable is true.

A compact line can be useful for a short expression, but squeezing branch logic into nested expressions often makes the order and result harder to follow. The examples below distinguish these forms so you can choose the one that matches your task.

Write Multiple if Statements on One Line in Python

A semicolon separates simple statements, such as first = 1; second = 2. It does not turn a compound if statement into a simple statement: writing if ready: start(); if finished: stop() on one physical line is not valid Python syntax. A single if suite can contain simple statements separated by semicolons, as in if ready: start(); log_status(), but this is rarely clearer than placing each action on its own indented line.

If you need to test alternative conditions and perform different actions, write an if/elif/else block. Python checks the conditions in order and executes only the suite belonging to the first true condition. The block is also easier to extend when the rules or actions change.

Use a Conditional Expression to Choose One Value

A conditional expression has the form value_if_true if condition else value_if_false. It is an expression, so it produces one value that can be assigned, returned, or passed to a function. It does not create multiple if statements or run separate branch suites.

For example, label = "adult" if age >= 18 else "minor" chooses one of two strings. To choose among three results, a nested conditional expression is possible: label = "low" if score < 50 else "pass" if score < 80 else "high". Read the nesting from left to right: if the first condition is false, Python evaluates the next conditional expression. Use this form only when the choices remain easy to scan; several nested conditions are usually clearer in a multiline branch block.

Use any() to Test Whether a Condition Is True

The built-in any() returns True when at least one item in its iterable is truthy, and returns False when no item is truthy. For example, any(score >= limit for limit in (50, 80)) checks whether a score reaches at least one of those limits. The generator expression supplies Boolean results one by one, and any() stops as soon as it finds a true result.

Use any() when the answer you need is a single yes-or-no value. It does not select which condition matched or choose a corresponding message. If each condition needs its own result or action, use if/elif/else instead. A list comprehension combined with any() is unnecessary when a generator expression can provide the Boolean checks directly.

Prefer a Multiline if/elif/else Block for Multiple Branches

For the comparison a = 2 and b = 3, this block prints the matching relationship.

a = 2
b = 3

if a < b:
    print("a is less than b")
elif a == b:
    print("a is equal to b")
else:
    print("a is greater than b")
a is less than b

The if and elif conditions are checked in order. Because the values satisfy a < b, the first suite prints its message and the remaining branches are skipped. The final else handles every other relationship, including a > b.

Use a conditional expression when you need to choose a small value, and use any() when you only need to know whether an iterable contains a true value. For multiple actions or several distinct outcomes, the multiline block makes the branch order and behavior explicit. Although semicolons can separate simple statements, they do not make a chain of compound if statements a good one-line alternative.