Ternary Operator in Python
Ternary Operator in Python
Ternary operators also known as conditional expressions are operators that evaluate
something based on a condition being true or false. It was added to Python in version 2.5.
It simply allows to test a condition in a single line replacing the multiline if-else making the
code compact.
Syntax :
print(min)
Output:
10
We use cookies to ensure you have the best browsing experience on our website. By using our
Output: Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
10
https://www.geeksforgeeks.org/ternary-operator-in-python/?ref=lbp 1/6
8/18/2020 Ternary Operator in Python - GeeksforGeeks
10
10
print ("Both a and b are equal" if a == b else "a is greater than b"
if a > b else "b is greater than a")
if a != b:
if a > b:
print("a is greater than b")
else:
print("b is greater than a")
else:
print("Both a and b are equal")
Important Points:
First the given condition is evaluated (a < b), then either a or b is returned based on the
Boolean value returned by the condition
Order of the arguments in the operator is different from other languages like C/C++
(See C/C++ ternary operators).
Conditional expressions have the lowest priority amongst all Python operations.
Method used prior to 2.5 when ternary operator was not present
In an expression like the one given below , the interpreter checks for the expression if this is
true then on_true is evaluated, else the on_false is evaluated.
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/ternary-operator-in-python/?ref=lbp 2/6
8/18/2020 Ternary Operator in Python - GeeksforGeeks
Syntax :
Example :
print(min)
Output:
10
Note : The only drawback of this method is that on_true must not be zero or False. If this
happens on_false will be evaluated always. The reason for that is if expression is true, the
interpreter will check for the on_true, if that will be zero or false, that will force the interpreter
to check for on_false to give the nal result of whole expression.
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/ternary-operator-in-python/?ref=lbp 3/6
8/18/2020 Ternary Operator in Python - GeeksforGeeks
This article is contributed by Mayank Rawat and improved by Shubham Bansal. If you like
GeeksforGeeks and would like to contribute, you can also write an article using
contribute.geeksforgeeks.org or mail your article to [email protected]. See
your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you nd anything incorrect, or you want to share more information
about the topic discussed above.
Ad
Recommended Posts:
Bene ts of Double Division Operator over Single Division Operator in Python
Operator Overloading in Python
Python | Operator.countOf
What is a modulo operator (%) in Python?
Operator Functions in Python | Set 2
Operator Functions in Python | Set 1
Difference between == and is operator in Python
Walrus Operator in Python 3.8
Python | List comprehension vs * operator
Concatenate two strings using Operator Overloading in Python
Check if a value exists in a DataFrame using in & not in operator in Python-Pandas
Division without using '/' operator
Operator Overloading in C++
New '=' Operator in Python3.8 f-string
Addition of two
We use cookies number
to ensure using
you have the'-' operator
best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Implement *, - and / operations using only + arithmetic operator
https://www.geeksforgeeks.org/ternary-operator-in-python/?ref=lbp 4/6
8/18/2020 Ternary Operator in Python - GeeksforGeeks
Improved By : nsuman35
18
2.1
To-do Done
Based on 32 vote(s)
Please write to us at [email protected] to report any issue with the above content.
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Load Comments
Company Learn
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
About
site, you acknowledge that youUs
have read and understood our Cookie Policy & PrivacyAlgorithms
Policy
https://www.geeksforgeeks.org/ternary-operator-in-python/?ref=lbp 5/6
8/18/2020 Ternary Operator in Python - GeeksforGeeks
Practice Contribute
Courses Write an Article
Company-wise Write Interview Experience
Topic-wise Internships
How to begin? Videos
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/ternary-operator-in-python/?ref=lbp 6/6