Ternary Operator in Python

In this Python tutorial, we will learn how to use the famous Ternary Operator in Python.

Table Of Contents

Let’s dive into the tutorial.

How to use Ternary Operator in Python?

Ternary Operator works in a single line. It takes condition and returns a value based on the condition. Like, C or C++, Python does not have any ternary operator. But we can achieve the same functionality in python using if-else in single line only.

Just like Ternary operator, we can use if-else to check the condition/expression and return the results in one line.

Syntax:

[true_block] if [condition/expression] else [false_block]

So, if the condition is True, then true_block will be executed, otherwise false_block is executed.

Example:

In this example, we will use Ternary Operator to check three different conditions.

# Create two integer variables
# and assign values to it
a=34
b=21

# Check whether a is greater than b
# using the ternary operator
print( "a is greater than b" if a > b else "a is not greater than b" )

# Check whether a is greater than or equal to b
# using the ternary operator
print("a is greater than or equal to  b" if a >= b else "a is neither greater nor equal to b")

# check whether a + b = 45
print("equal to 45" if a + b == 45 else "not equal to 45")

Output:

a is greater than b
a is greater than or equal to  b
equal to 45

Here, we are

  1. Checking if a is greater than b using ternary operator – Execute True block since a is greater than b.
  2. Checking if a is greater than or equal to b using ternary operator – Execute True block since a is greater than or equal to b.
  3. Checking if a+b == 45 – Execute False block since a+b == 55.

Implementing Nested Ternary Operator in Python

It can be possible to implement a Nested Ternary operator using nested if-else. Inside false-block or else-block, we will again check the if-else conditions.

Syntax:

[true_block] if [condition/expression] else [true_block] if [condition/expression] else [false_block].................

Example:

In this example, we will use Ternary Operator to check three different conditions.

# Create two integer variables and 
# assign values to it
a=400
b=400

# Check whether a is equal to b or not 
# If it is not equal, then check for conditions in the else block
print("a is equal to b" if a==b else "a is less than b" if a<b else "a is not less than b" )

a=400
b=200

# Check whether a is equal to b or not 
# If it is not equal, then check for conditions in the else block
# In else block - else block will execute since a is not less than b
print("a is equal to b" if a==b else "a is less than b" if a<b else "a is not less than b" )

a=100
b=200

# Check whether a is equal to b or not 
# If it is not equal, then check for conditions in the else block 
# In else block - if block will execute since a is  less than b
print("a is equal to b" if a==b else "a is less than b" if a<b else "a is not less than b" )

Output:

a is equal to b
a is not less than b
a is less than b
  1. For the first nested ternary operator, (True_block)if block is executed.
  2. For the second nested ternary operator, In the else block – the else block will execute since a is not less than b.
  3. For the third nested ternary operator, In else block – if block will execute since a is less than b.

Implementing Ternary Operator using Tuple

Using the tuple data structure, we can implement a ternary operator. A Tuple in Python, is represented by (). The True_block and false_block are placed inside the tuple.

Syntax:

(false_block, true_block) [condition/expression]

The false_block will execute when condition/expression is False, otherwise, true_block is executed.

Example:

In this example, we will check different conditions using a tuple.

# create two integer variables and assign values to it
a=800
b=500

# check whether a is greater than b
print(("a is not greater than b", "a is greater than b") [a>b])

a=800
b=900

# check whether a is greater than b
print(("a is not greater than b", "a is greater than b") [a>b])

a=800
b=700

# check whether a+b equals 1500
print(("Not equal to 1500", "Equals 1500") [a+b==1500])

Output:

a is greater than b
a is not greater than b
Equals 1500

In this code, we are

  1. Checking whether a is greater than b – Execute true_block
  2. Checking whether a is greater than b – Execute false_block
  3. Checking whether a+b equals 1500 – Execute true_block

Implementing Ternary Operator using Dictionary

Using dictionary data structure, we can implement ternary operator. A Dictionary in Python is represented by {}. It stores key valuer pairs. The True_block and false_block are placed inside the dictionary as values.

  • For key – True: The true_block will be the value.
  • For key – False: The false_block will be the value.

Syntax:

{False:"false_block", True:"true_block"} [condition/expression]

false_block will execute when condition/expression is False, otherwise, true_block is executed.

Example:

In this example, we will check different conditions using the dictionary.

# create two integer variables and assign values to it
a=800
b=500

# check whether a is greater than b
print({False:"a is not greater than b",True: "a is greater than b"}[a>b])

a=800
b=900

# check whether a is greater than b
print({False:"a is not greater than b",True: "a is greater than b"}[a>b])

a=800
b=700

# check whether a+b equals 1500
print({False:"Not equal to 1500", True:"Equals 1500"} [a+b==1500])

Output:

a is greater than b
a is not greater than b
Equals 1500

In this code, we are

  1. Checking whether a is greater than b – Execute true_block
  2. Checking whether a is greater than b – Execute false_block
  3. Checking whether a+b equals 1500 – Execute true_block

Summary

From the above article, we have seen what is a Ternary operator and we can also implement a nested Ternary operator in Python. A Tuple or Dictionary can be also be used to implement the Ternary operator in Python. Happy Learning.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top