python – Is it possible to shorten conditional assignment

Question:

There is such a code

x = 3 * y + z
x = x if x > 0 else 0

Is it possible to somehow write this into one instruction, but avoid duplicate computation of x ? Those. not this way:

x = (3 * y + z) if (3 * y + z) > 0 else 0

Answer:

I would write max(0, 3 * y + z)

Scroll to Top