Ternary Operators in Python

Ternary Operators in Python

2 mins read899 Views Comment
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.

2022_07_ternary-operator-1.jpg

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.

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

Free
6 weeks
โ€“ / โ€“
3 months
โ€“ / โ€“
2 weeks
โ€“ / โ€“
16 weeks
โ‚น1.7 K
3 months
โ€“ / โ€“
โ€“ / โ€“
โ‚น4.24 K
2 weeks
โ‚น3 K
3 weeks
โ€“ / โ€“
4 months

Ternary Operator Syntax

Hereโ€™s the syntax for a ternary operator in Python:

[on_true] if [expression] else [on_false] 

explain

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 = 30
num2 = 50
#Use ternary operator
max = num1 if num1 > num2 else num2
print(max)
Copy code

Output:

A picture containing logo

Description automatically generated

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 = 30
num2 = 50
#Use ternary operator
print("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 = 30
num2 = 50
#Use ternary operator
print(num1,"is greater") if (num1 > num2) else print(num2,"is greater")
Copy code

Output:

Advantages of ternary operators over if-else statements?

Ternary operators have two main advantages over the traditional 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.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio