Ternary Operators in Python

# Ternary Operators in Python

clickHere
Updated on Nov 22, 2022 19:35 IST

For a Python programmer, one of the major concerns is the size and readability of the code. Any efficient program in Python is supposed to be easily readable, short, and clean. This is just what ternary operators are used for.

In Python, ternary operators offer a convenient option for making the code more compact and easier to understand. In this article, we will learn how this happens.

We will be covering the following sections today:

## What are Ternary Operators?

Ternary operators in Python are conditional expressions that return Boolean values (True or False) based on whether the given conditions are met or not.

These are basically a single-line form of the if-else statements. Hence, these operators make our code more compact and improve their speed of execution.

## Ternary Operator Syntax

Here’s the syntax for a ternary operator in Python:

[on_true] if [expression] else [on_false]

The three operands in a ternary operator include:

• condition: A Boolean expression that evaluates whether it is True or False.
• true_val: Executes the true_val statement if the condition is True.
• false_val: Execute the false_val statement if the condition is False.

Now, let’s look at the Python implementation of ternary operators

Example:

Let us consider an example to check which of the two input numbers is greater:

` `
`num1 = 30num2 = 50 #Use ternary operatormax = num1 if num1 > num2 else num2 print(max)Copy code`

Output:

As you can see from the code above, we used the ternary operator to check whether num1 > num2, since the condition is False, we displayed the false_val, i.e., num2 which is 50.

## Ternary Operator as nested if-else

Let us consider the following example that demonstrates how nested if-else can be written as a ternary operator:

` `
`num1 = 30num2 = 50 #Use ternary operatorprint("Both num1 and num2 are equal" if num1 == num2 else "num1 is greater than num2" if num1 > num2 else "num2 is greater than num1")Copy code`

Output:

The above Python code checks the condition and executes the statement associated with it:

• “Both num1 and num2 are equal” – this will be printed if the condition num1 == num2 is true.
• “num1 is greater than num2” – this will be printed if the condition num1 > num2 is true.
• “num2 is greater than num1” – this will be printed if the condition num2 > num1 is true.

## Ternary Operator with Print Function

We can directly use the print statement with ternary operators as well. Let us consider the following example:

` `
`num1 = 30num2 = 50 #Use ternary operatorprint(num1,"is greater") if (num1 > num2) else print(num2,"is greater")Copy code`

Output:

## Advantages of ternary operators over if-else statements?

• Single-line expression makes the code execute faster and more compact.
• Increases code readability by reducing the number of code lines.

But whether or not you want to include ternary operators in your code is completely your choice.

## Endnotes

Python. We learned how these operators are used to replace the if-else conditions in our Python code, thereby making it more efficient and readable. If you wish to learn more about Python and practice Python programming, you can explore related articles here.