
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Ternary Conditional Operator
The Python ternary operator returns a value based on whether a condition is True or False. It is similar to an if-else statement but is expressed in a single line.
For understanding the ternary operator, we need to have an idea about conditional statements. Let's have a basic understanding of the if-else statement. We will have an if block followed by an else block here. The if block is executed when the given condition is True, and the else block is executed if the given condition is False.
The following is the basic syntax of the if-else statement -
if condition: statement1 statement2 else: statement3 statement4
Let's understand the if-else statement with the following example. Here, the value of var is greater than zero; this condition returns true, so the if block is executed -
var = 14 if var>0: print("The given condition is True") else: print("The given condition is False")
Following is the output of the above code -
The given condition is True
What is a ternary operator?
The ternary operator allows us to evaluate the given condition if it is True or False. It contains two sets of statements, the first set (say true_value) is followed by the if keyword, a condition, the else keyword, and then the second set of statements (say false_value).
If the given condition is True, this operator will return true_value; else, it will return false_value. This operator is concise and can replace an if-else statement in a single line of code.
Syntax of Ternary Operator
Following is the syntax of the Python Ternary Operator -
true_value if condition else false_value
Example : Ternary Operator
Following is a basic example of the Python ternary operator -
var_1 = 10 var_2 = 45 result ="var_1 is less than var_2" if 10 < 24 else "var_2 is greater than var_1" print(result)
Output
Following is the output of the above code -
var_1 is less than var_2
Ternary Operator with different data types
The ternary operator can be used with different data types -
- Using tuples
- Using dictionary
- Using lambda()
Using Python Tuples
The ternary operator can be used with Python tuples. The 0 and 1 index values are specified with false_value and true_value values, respectively. If the given condition is True, it will return the value at index 1. If the given condition is False, then it will return the value at the 0 index.
Syntax
Following is the syntax of the Python ternary operator using a tuple -
(false_value, true_value)[condition]
Example
Let's understand the usage ternary operator with tuples -
var_1 = 15 var_2 = 0 result = ("var_1 and var_2 : False","var_1 and var_2 : True")[ var_1 and var_2] print(result)
Output
Following is the output of the above code -
var_1 and var_2 : False
Using Python dictionary
The ternary operator can be used with a Python dictionary. The keys are True and False, and the values are true_value and false_value. If the condition is True, the value corresponding to the True key is returned; otherwise, the value corresponding to the False key is returned.
Syntax
Following is the syntax of the Python ternary operator using dictionary ?
{True:true_value, False:false_value}[condition]
Example
Following is an example of the Python ternary operator using dictionary ?
var_1 = 1 var_2 = 0 result = {True:"var_1 or var_2 : True",False:"var_1 or var_2 : False"}[var_1 or var_2] print(result)
Output
Following is the output of the above code -
var_1 or var_2 : True
Using lambda
We can also use the ternary operator with lambda. This is similar to a tuple where false_value is at 0 index and true_value at 1. If the condition is True it will return the index 1 value. Otherwise, the value corresponds to the 0 index.
Syntax
Following is the syntax of the Python ternary operator using lambda -
(lambda : false_value, lambda : true_value)[condition]()
Example
In the following example we have performed the ternary Operator using lambda -
var_1 = 8 result = (lambda : "False",lambda : "True")[var_1 < 0]() print(result)
Output
Following is the output of the above code -
False
Nested Ternary Operator
The ternary operator can be used instead of a nested if-else statement. If we use a ternary operator inside another ternary operator, it is known as a nested ternary operator.
Syntax
Following is the syntax of the Python nested ternary operator -
true_value if condition1 else (true_value if condition2 else false_value)
Example
Following is an example of the Python nested ternary operator -
var_1 = 50 var_2 = 80 print("Both are equal" if var_1 == var_2 else "var_1 is greater" if var_1 > var_2 else "var_2 is greater")
Output
Following is the output of the above code -
var_2 is greater