How do you write ternary operator without using if else keywords?

Using and and or conditions:

<condition> and <expression_on_true> or <expression_on_false>

Using tuple, dictionary or lambda expressions:


{True:<expression_on_true>, False: <expression_on_false>}[condition]
# using Lambda
result = (lambda: "Odd", lambda: "Even")[n % 2 == 0]()

For more details with examples, read article: Python: Ternary Conditional Operator

Leave a Reply

Your email address will not be published. Required fields are marked *