4 ms·
Conditional expressions/operators, including e.g. the `?:` ternary operator in C-like languages, typically have about the lowest precedence, higher only than th
by jolmg 1y ago
Conditional expressions/operators, including e.g. the `?:` ternary operator in C-like languages, typically have about the lowest precedence, higher only than the assignment operators (and the comma operator in C-like languages). It's not just a Python thing; you'll find `+` having higher precedence basically everywhere.
Think of
x = 1 + 2 if True else 3
like a shorthand for:
if True:
x = 1 + 2
else:
x = 3
which can be a common pattern in languages that don't really have conditional expressions, like bash.