Ternary Operator in Python with Examples
Python’s if-else statements are very easy to write and read. But they come with a downside. That is, if you want to print something based on some condition, you need 4 lines to do that.
x = 10 if x > 5: print("greater") else: print("smaller")
That’s an awful lot of lines for such a simple operation. What’s the solution, you ask? Ternary operators in Python!
Let’s learn more about Python ternary operators.
What is Ternary Operator in Python?
The ternary operator in Python is nothing but a one-line version of the if-else statement. It provides a way to write conditional statements in a single line, replacing the multi-line if-else syntax.
Syntax of Python ternary operator
<true_value> if <conditional_expression> else <false_value>
Python ternary operator works with three operands:
1. conditional_expression: This is a boolean condition that evaluates to either true or false.
2. true_value: The value returned by the ternary operator if the conditional_expression evaluates to True.
3. false_value: The value returned by the ternary operator if the conditional_expression evaluates to False.
Example 1: Conditional expression is a boolean value
>>> nice_weather = True >>> print("Go out for a walk" if nice_weather else "watch a movie at home")
Output:
>>> nice_weather = False >>> print("Go out for a walk" if nice_weather else "watch a movie at home")
Output:
Example 2: Conditional expression is a boolean expression
>>> num = 12 >>> print("Even" if num % 2 == 0 else "Odd")
Output:
Example 3: Variable assignment
>>> a = 10 >>> b = 5 >>> largest = a if a > b else b >>> print(largest)
Output:
>>>
The variable largest gets the value of:
- a, if a is greater than b
- b, if a is not greater than b
The ternary operator using Tuple
Another way to implement ternary operation in Python is by using a tuple. This is a simple replacement for the if-else ternary operator.
Syntax
(false_value, true_value)[conditional_expression]
In this case, the false_value and true_value form the two elements of the tuple. And the conditional_expression goes within the square bracket notation in place of an index. This works simply because the value of True is 1 and that of False is 0.
Therefore:
- If the conditional_expression evaluates to True, it’s value becomes .
- Then the element at index 1 gets returned by the ternary expression.
- And if the conditional_expression evaluates to False, it’s value becomes 0.
- Then the element at index 0 gets returned.
Be sure to put the false_value at index 0 and true_value at index 1 in the tuple.
>>> a = 10 >>> b = 5 >>> largest = (b, a)[a > b] >>> print(largest)
Output:
>>> nice_weather = True >>> print(("Watch a movie at home", "Go out for a walk")[nice_weather])
Output:
>>>
We can extend this syntax and use a list or a dictionary instead of a tuple.
The syntax for ternary operation using a list
[false_value, true_value][conditional_expression]
Example:
>>> a = 10 >>> b = 5 >>> largest = [b, a][a > b] >>> print(largest)
Output:
Syntax for ternary operation using a dictionary
{True: true_value, False: false_value}[conditional_expression]
Here, the true_value and false_value become the values of the keys True and False respectively. If the conditional_expression evaluates to True, the value corresponding to True gets returned. Else, the value corresponding to False gets returned.
Example:
>>> nice_weather = False >>> s = {True: "Go out for a walk", False: "Watch a movie at home"}[nice_weather] >>> print(s)
Output:
>>>
Summary
This brings us to the end of the article. We learned about various ways to implement ternary operations in Python. We saw how ternary operators increase the code readability and make it compact.
Make use of ternary operators in your code to make it more “Pythonic”!