We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Python: if-else in one line – ( A Ternary operator )

In this article, we will learn how to use if-else in one line in python.

Other programming languages like C++ and Java have ternary operators, which are useful to make decision making in a single line. Python does not have a ternary operator. But in python, we can use the if-else in a single line, and it will give the same effect as the ternary operator.

Advertisements

So, let’s see how to use if-else in one line,

if..else in a single line in python like a ternary operator

In python, we can convert the if…else statement to a one-line conditional expression. For this, we need to write it in a specific format, checkout its syntax,

Syntax of if…else in one line or ternary operator

value_1 if condition else value_2

When the condition evaluates to True, then the result of this one-liner if..else expression will be value_1. Whereas, if the condition evaluates to False, then the result of this one-liner expression will be value_2. Let’s see some examples of it.

Example of if…else in one line

'High' if x > 10 else 'Low'

In this one-liner expression, we are using an if…else statement in a single line.

  • If the value of x is greater than 10, then the expression will return ‘High’.
  • If the value of x is less than 10, then the expression will return ‘Low’.

We can assign the value returned by the expression to another variable.
Now let’s see how to use this one liner if else expression,

Example 1:

x = 18

result = 'High' if x > 10 else 'Low'

print(result)

Output:

High

Here the value of x was 18, which is greater than 10. So one-liner if-else expression evaluated to ‘High’ and got assigned to variable result.

Example 2:

x = 5

result = 'High' if x > 10 else 'Low'

print(result)

Output:

Low

Here the value of x was 5, which is less than 10. So one-liner if-else expression evaluated to ‘Low’ and got assigned to variable result.

Use if…else statement in one line carefully

While using the if-else statement in one line, we should prefer using parenthesis to avoid confusion. Checkout following example,

x = 20

result = 10 + 10 if x > 100 else 0

print(result)

Output:

0

In this example, the conditional expression x>100 evaluated to False, and the value of the else portion, i.e., 0 was returned.

But what is the value of if portion in the above example? Is it 10 or 10 + 10 ?

The value of if-portion is 10+10, i.e., 20.

But it is a little confusing here because some might think that the if..else expression is 10 if x > 100 else 0  and then 10 to the value returned by expression. By this logic if x is less than 100, then the value of this expression should be 10. But that is wrong in this case.

So, to avoid such kind of confusion, we should proper brackets/parenthesis while using if…else in one line, like this,

x = 20

result = 10 + (10 if x > 100 else 0)

print(result)

Output:

10

Now in this one-liner if-else statement, the value of the if-portion is 10, and the value in else-portion is 0.

Conclusion:

Although python does not have a ternary operator, but still using if-else in one line, we can have an effect of a ternary operator in python.

Cart

Your Cart is Empty

Back To Shop