Why we need Ternary Operator in python?

If-else statements in Python are fairly simple to construct and read. They do, however, have a drawback. In other words, four lines are required to print something based on a condition.

x = 20
if x > 10:
    print("greater")
else:
    print("smaller")

There are just too many lines for such a straightforward task. You ask, "What's the solution?" Python's ternary operators

What is Ternary Operator in Python?

The Python ternary operator is nothing more than an if-else statement condensed into a single line. It offers an alternative to the multi-line if-else syntax that allows conditional statements to be written on a single line.

Syntax :

<true_value> if <conditional_expression> else <false_value>

The ternary operator in Python has three operands:

1. Conditional expression: This is a boolean expression that can be either true or false when evaluated.

2. True value: The amount returned by the ternary operator when the conditional expression evaluates to True.

3. False value: The value that the ternary operator returns when the conditional statement 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:

Go out for a walk

nice_weather = False
print("Go out for a walk" if nice_weather else "watch a movie at home")

Output:

watch a movie at home

Example 2: Conditional expression is a boolean expression

num = 14
print("Even" if num % 2 == 0 else "Odd")

Output:

Even

Example 3: Variable assignment

a = 20
b = 10
largest = a if a > b else b
print(largest)

Output:

20

The variable largest gets the value of:

  • a, if a is greater than b
  • b, if a is not greater than b

Utilising Tuple, Ternary Operator

Using a tuple is an additional method for implementing ternary operations in Python. The if-else ternary operator can be easily replaced with this.

Syntax :

(false_value, true_value)[conditional_expression]

In this instance, the two components of the tuple are false value and true value. And in instead of an index, the conditional expression is enclosed in square bracket notation. Simply because True has a value of 1, and False has a value of 0, this works.

Therefore:

  • The value is whether the conditional expression evaluates to True.
  • The ternary expression then returns the element at index 1.
  • And its value becomes 0 if the conditional expression evaluates to False.
  • The element with index zero is then returned.

Ensure that 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:

10

nice_weather = True
print(("Watch a movie at home", "Go out for a walk")[nice_weather])

Output:

Go out for a walk

We can extend this syntax and use a list or a dictionary instead of a tuple.

Using a list, Ternary Operation

[false_value, true_value][conditional_expression]

Example:

a = 10
b = 5
largest = [b, a][a > b]
print(largest)

Output:

10

Using a dictionary, Ternary Operations

{True: true_value, False: false_value}[conditional_expression]

Here, the true value and false value take the place of the respective values for the keys True and False. The value corresponding to True is returned if the conditional expression evaluates to True. In all other cases, the False value is returned.

Example

nice_weather = False
s = {True: "Go out for a walk", False: "Watch a movie at home"}[nice_weather]
print(s)

Output:

Watch a movie at home

Conclusion

We have now reached the end of the article. We learned about numerous Python ternary operator implementation strategies. We observed how ternary operators improve the readability and compactness of the code.

To make your code more "Pythonic," utilise ternary operators!