What is Ternary Operator and How to use it in Python : Examples

What is Ternary Operator and How to use it in Python

Python is a very easy-to-learn programming language. You can implement or automate many tasks using it in less time. The ternary operator in python is one of them. In this entire tutorial, you will learn how to implement the ternary operator in python using various examples.

What is the Ternary operator in python?

You must have knowledge of the if else condition block. The ternary operator is similar to it. It allows you to return the value based on the result of the binary condition. The syntax of this ternary operator is below.

condition? expression_if_true : expression_if_false

Here you can see the before the ” ? “, you put your condition and after it the result. The result after the colon(:) you will get when the condition is false.

In the next section, you will implement the ternary operator in python with examples.

Examples of the ternary operator in python

Let’s know the various examples of ternary operators and how to use them.

Example 1: Simple ternary operator in python

A simple example to use the ternary operator is a single condition. Here you check for only one condition and then return the value based on it.

For example, I want to print whether the number is negative or positive. To do so I have to check whether the number is less than 0 or not.

Run the below lines of code to perform the simple ternary operator.

x= 10
print("The number is positive") if x > 0 else print("The number is negative")

Output

Simple use of ternary operator
Simple use of ternary operator

Example 2: Nested Ternary Operator

You can also use more than one condition to perform the ternary operator. For example, I want to check whether the number is greater than 10 and whether it is positive or not. To achieve that you have to use the nested ternary operator.

Execute the below lines of code and see the output.

x =11
print("The number is positive and greater than 10")if x>10 else print("The number positive and less than 10")
 if x >0 else print("The number is negative")

Output

Nested Ternary Operator
Nested Ternary Operator

How to convert IF-ELSE to the ternary operator

Suppose your existing code contained an if-else block then you can easily convert it into a Ternary block.

IF-ELSE statement Block

x = 10
if x >0:
  print("The number is positive")
else:
  print("The number is negative")

Ternary operator Block

x= 10
print("The number is positive") if x > 0 else print("The number is negative")

In both cases, you will get the same output. The number is positive

Conclusion

The python ternary operator is very useful for the developer to save time while coding. It replaces the IF-ELSE block in a single line of statements. These are examples of the implementations of the ternary operator.

I hope you have understood how to use the ternary operator. If you have still a query then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner