Python ? (conditional/ternary) operator for assignments [duplicate] Python ? (conditional/ternary) operator for assignments [duplicate] python python

Python ? (conditional/ternary) operator for assignments [duplicate]


Python has such an operator:

variable = something if condition else something_else

Alternatively, although not recommended (see karadoc's comment):

variable = (condition and something) or something_else


In older Python code, you may see the trick:

condition and something or something_else

However, this has been superseded by the vastly superior ... if ... else ... construct:

something if condition else something_else