how to write if else statement in one line python code example
Example 1: python if else one line
x = 'foo' if bar else 'baz'
Example 2: python if elif else in one line
>>> i=100
>>> a = 1 if i<100 else 2 if i>100 else 0
>>> a
0
>>> i=101
>>> a = 1 if i<100 else 2 if i>100 else 0
>>> a
2
>>> i=99
>>> a = 1 if i<100 else 2 if i>100 else 0
>>> a
1