# How can we use Python Ternary Operator Without else?

PythonProgramming

If you want to convert a statement like −

if <condition>:
<some-code>

to a single line, You can use the single line if syntax to do so −

if <condition>: <some-code>

Another way to do this is leveraging the short-circuiting and operator like −

<condition> and <some-code>

If <condition> is false, then short-circuiting will kick in and the right-hand side won't be evaluated. If <condition> is true, then the right-hand side will be evaluated and <some-code> will be evaluated.

Published on 19-Apr-2018 12:32:51