The ternary conditional operator in Python

The ternary operator is very useful for a single condition. But support ternary operator different way, not like other programming languages (?:).

Syntax: A if C else B

This first evaluates C; if it is true, A is evaluated to give the result, otherwise, B is evaluated to give the result.

Example

>>> 'true' if True else 'false'
'true'
>>> 'true' if False else 'false'
'false'

Also Read : Python Interview Question

Leave a Reply