Ternary Expressions
if ... else can be used to write a ternary expression. It does exactly what is sounds like: assign 3 to x if y is 1
. The parens are not needed but they are good for readability.
>>> y = 0
>>> x = 3 if (y == 1) else 2
>>> print(x)
2