Python If-Else on One Line

In Python, you can have if-else statements on one line.

To write an if-else statement on one line, follow the conditional expression syntax:

some_expression if condition else other_expression

For example:

age = 20

# One-liner if-else statement
age_group = "Minor" if age < 18 else "Adult"


This is handy with short if-else statements because it allows you to save lines of code while preserving code quality.

But do not overuse it.

Turning longer if-else statements into one-liners can make your code unreadable.

In this guide, you are going to learn how to turn if-else statements into one-liner expressions in Python. More importantly, you are going to learn how to do it wisely.

If you are interested in learning other Python tips and “tricks”, feel free to read this article.

One Line If-Else Statements in Python

Writing a one-line if-else statement in Python is possible by using the ternary operator, also known as the conditional expression.

Here is the syntax of the one-liner ternary operator:

some_expression if condition else other_expression

For instance, let’s say you have an if-else statement that checks if a person is an adult based on their age:

age = 20

if age < 18:
    age_group = "Minor"
    age_group = "Adult"




This works just fine. But you can get the job done by writing the if-else statement as a neat one-liner expression.

For example:

age = 20
age_group = "Minor" if age < 18 else "Adult"




As you can see, you were able to save three lines of code without sacrificing the code readability.

One Line If-Elif-Else in Python

Fitting everything in one line is not good practice. More importantly, a Python code line should not exceed 80 characters in general.

This is why Python does not support the if-elif-else statements as one-liner expressions.

If you really want to push it, you can chain ternary conditional operators to achieve the if-elif-else behavior:

n = 10

a = 1 if n < 10 else 2 if n > 10 else 0

But please, do not do this. As you can see from the above, it only makes the code less readable.

It is way cleaner to write the above expression like this:

n = 10

if n < 10:
    a = 1
elif n > 10:
    a = 2
    a = 0

Some people do not use one-liner if-else statements at all. This is because it is up to a debate as to whether it improves code quality or not.

Be Careful with If-Else on One Line

One-line if-else statements should only be used with simple expressions (identifiers, literals, and operators). They should not be used with longer statements.

This is to preserve the readability and expressibility of the code. So think twice before breaking your Python if-else on one line.

You already saw a bad example of a lengthy one-liner if-elif-else statement in the previous section. Let’s see a bad example of a one-liner if-else statement as well.

First, let’s use a regular if-else approach:

x = 1

if x % 2 == 0:
    result = x * 2 + 10
    result = x / 2 - 10

This looks clear. If x is even, multiply it by 2 and add 10 to it. If the number is odd, divide it by 2 and subtract 10.

But let’s then take a look at what happens when you turn it into a one-liner expression:

result = x * 2 + 10 if x % 2 == 0 else x / 2 - 10

Total chaos. Nobody wants to read code like this.

This is a perfect example of how using one line if-else can mess things up. So be smart!


In Python, you can turn if-else statements into one-liner expressions using the ternary operator (conditional expression).

Using the ternary conditional operator in Python follows this syntax:

some_expression if condition else other_expression

As an example, you can perform a simple age check with a shorthand if-else statement:

age = 12
age_group = "Minor" if age < 18 else "Adult"

Using a one-liner if-else statement makes sense if it improves the code quality. But it can work against you if you write complex statements as one-liners.

Thanks for reading. Happy coding!

Further Reading

50 Python Interview Questions with Answers

50+ Buzzwords of Web Development