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.


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 


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
Copy code


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


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


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. 


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