Explore
ProGrad Programs
Placement Prep
TCS Codevita
Live Placement Training
Live Aptitude Training
Live Programming Training
Webinars
About Us

Edit
Reply




Edit

Ternary Operator In Python or Conditional Operator | One Line if statement

Published on 09 Mar 2020

Ternary operator in Python is the equivalent of if else statement. The ternary operator allows testing a condition in a single line, replacing the multi-line if-else and making the code more compact. It also increases the readability of code.


ternary operator in python


There is no keyword for implementing a ternary operator.Below is the syntax of the ternary operator in Python.


Syntax for Ternary Operator


Syntax for implementing if else using Ternary operator:In this syntax, whenever the given condition isTrue, statement given in [on_True] get executed & whenever the condition isFalse, [on_False] statement gets executed.


[on_True] if (condition) else [on_False]


Syntax for implementing if elif else using Ternary operator: In this syntax, when condition1 isTrue, the statement given in [on_True1] gets executed. But when the condition isFalse, the control shifts to else and here condition2 is checked. If condition2 isTrue, then [on_True2] gets executed and if not, the statements in [on_False1] get executed.


[on_True1] if(condition1) else [on True2] if(condition2) else [on False1]


Ways of Implementing Ternary Operator in Python


Let us look at some example codes and methods in which multi-line if-else can be reduced into single line ternary operation using the above syntax.


a) Simple Method for Implementing Ternary Operators


#program to compare a & b
a, b = 7, 8
print("a is less than b") if(a < b) else print("a is greater than b")


Output: a is less than b


#program to compare a & b
a, b = 7, 7
print("a is less than b") if(a<b) else print("a is equal to b") if(a == b) else print("b is less than a")


Output: a is equal to b


b)Ternary Operation Using Tuple


When implementing using Tuples, statements or the body of if becomes elements of the Tuple enclosed within round brackets and separated by a comma.


Syntax for Ternary Operator using Tuple:


(on False, on True) [condition]


Here,on Falseandon Trueare two elements of the tuple. Element at the first index is accessed when the given condition becomesFalseand element at the second index is accessed when the given condition becomesTrue. For example,


#program to check if a student cleared the test
score = 65
result = ("Fail", "Pass") [score > 50]
print (result)


Output: Pass


Explanation:Since the value in the operand 'score' is greater than 50, the second element of the tuple gets printed.


c)Ternary Operation Using List


While using Lists, statements are the elements of the list enclosed within square brackets and separated by a comma.


Syntax for Ternary Operation using List:


[on False, on True][condition]


Here,on Falseandon Trueare two elements of the list. Element at the first index is accessed when the given condition becomesFalseand element at the second index is accessed when the given condition becomesTrue. For instance,


#program to check if a student cleared the test
score = 75
res = ["Fail", "Pass"][score > 75]
print (res)


Output: Fail


Explanation:Since the value in the operand 'mark' is 75, the first element of the list gets printed.


d)Ternary Operation Using Dictionary


Dictionaries can also be used to implement the ternary operation. But in this case,statements are stored as values of the keys True & False enclosed within square brackets and separated by a comma.


Syntax for Ternary Operation using List:


{True: on True, False: on False} [condition]


Here, 'on False' and 'on True' are two values of the keys True & False. The value of the first key gets accessed when the given condition becomesTrueand value of the second key gets accessed when the given condition becomesFalse. For instance,


#program to check if a student cleared the test
score = 76
print ({True: "Pass", False: "Fail"} [score > 75])


Output: Pass


e)Ternary Operation Using Lambda Functions


This will be discussed once we master the Lambda functions module.


Ternary Operator in Python FAQs


1) Can ternary operators be used in Python?

Yes! Ternary operators can be used in Python. They perform the function of if-else, but unlike if-else, ternary operators are single line statements.


2) How to write ternary operator in Python

In this article, we have discussed the syntax for implementing a ternary operator in Python. The syntax is given below.

[on_True] if (condition) else [on_False] #for implementing a if-else
[on_True1] if(condition1) else [on True2] if(condition2) else [on False1] #for implementing a elif
If you have any feedback about this article and want to improve this, please write to enquiry@faceprep.in
Explore 'python'
Articles