Conditional Expression (Ternary Operator) in Python

Modified: | Tags: Python

Python has a conditional expression (sometimes called a "ternary operator"). You can write operations like if statements in one line with conditional expressions.

See the following article for if statements in Python.

Basics of the conditional expression (ternary operator)

In Python, the conditional expression is written as follows.

X if condition else Y

The condition is evaluated first. If condition is True, X is evaluated and its value is returned, and if condition is False, Y is evaluated and its value is returned.

If you want to switch the value based on a condition, simply use the desired values in the conditional expression.

a = 1
result = 'even' if a % 2 == 0 else 'odd'
print(result)
# odd

a = 2
result = 'even' if a % 2 == 0 else 'odd'
print(result)
# even

If you want to switch between operations based on a condition, simply describe each corresponding expression in the conditional expression.

a = 1
result = a * 10 if a % 2 == 0 else a * 100
print(result)
# 100

a = 2
result = a * 10 if a % 2 == 0 else a * 100
print(result)
# 20

An expression that does not return a value (i.e., an expression that returns None) is also acceptable in a conditional expression. Depending on the condition, either expression will be evaluated and executed.

a = 1
print('even') if a % 2 == 0 else print('odd')
# odd

The above example is equivalent to the following code written with an if statement.

a = 1

if a % 2 == 0:
    print('even')
else:
    print('odd')
# odd

You can also combine multiple conditions using logical operators such as and or or.

a = -2
result = 'negative and even' if a < 0 and a % 2 == 0 else 'positive or odd'
print(result)
# negative and even

a = -1
result = 'negative and even' if a < 0 and a % 2 == 0 else 'positive or odd'
print(result)
# positive or odd

if ... elif ... else ... by conditional expressions

By combining conditional expressions, you can write an operation like if ... elif ... else ... in one line.

X if condition1 else Y if condition2 else Z

However, it is difficult to understand, so it may be better not to use it often.

a = 2
result = 'negative' if a < 0 else 'positive' if a > 0 else 'zero'
print(result)
# positive

a = 0
result = 'negative' if a < 0 else 'positive' if a > 0 else 'zero'
print(result)
# zero

a = -2
result = 'negative' if a < 0 else 'positive' if a > 0 else 'zero'
print(result)
# negative

The following two interpretations are possible, but the expression is processed as the first one.

1. X if condition1 else (Y if condition2 else Z)
2. (X if condition1 else Y) if condition2 else Z

In the sample code below, which includes three expressions, the first expression is interpreted like the second, rather than the third:

a = -2
result = 'negative' if a < 0 else 'positive' if a > 0 else 'zero'
print(result)
# negative

result = 'negative' if a < 0 else ('positive' if a > 0 else 'zero')
print(result)
# negative

result = ('negative' if a < 0 else 'positive') if a > 0 else 'zero'
print(result)
# zero

List comprehensions and conditional expressions

By using conditional expressions in list comprehensions, you can apply operations to the elements of the list based on the condition.

l = ['even' if i % 2 == 0 else i for i in range(10)]
print(l)
# ['even', 1, 'even', 3, 'even', 5, 'even', 7, 'even', 9]
l = [i * 10 if i % 2 == 0 else i for i in range(10)]
print(l)
# [0, 1, 20, 3, 40, 5, 60, 7, 80, 9]

See the following article for details on list comprehensions.

Lambda expressions and conditional expressions

Conditional expressions are also useful when you want to apply an operation similar to an if statement within lambda expressions.

get_odd_even = lambda x: 'even' if x % 2 == 0 else 'odd'

print(get_odd_even(1))
# odd

print(get_odd_even(2))
# even

In the example above, the lambda expression is assigned to a variable for convenience, but this is not recommended by PEP8.

Refer to the following article for more details on lambda expressions.

Related Categories

Related Articles