Reading Time: 1 minutes
Ternary Operator/Conditional Expression in Python
A Ternary Operation involves 2 operators and 3 operands. Python offers a shorthand for if-then-else constructs using its ternary operator x if condition else y. It is also known as a conditional expression.
>>> day = 'Monday' >>> workHours = '9 to 5' if day in weekdays else '9 to 1' >>> workHours '9 to 5' >>> a, b = 10, 12 >>> larger = a if a > b else b >>> larger 12
For more on how this ternary operation is actualized in other languages, check out this article.