There is such a code

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

Is it possible to somehow write it into one instruction, but avoid duplicating the calculation of x ? Those. not in this way:

 x = (3 * y + z) if (3 * y + z) > 0 else 0 
  • What for? No need to try to stuff as much as possible into one line. The code must remain readable. - Xander
  • @ Alexander Just what I had was not good either. Either 2 lines of code for, one might say, one expression, or one line, but with not obvious code that contains duplicate calculations, which in itself is a source of possible errors in the future. - Edward Izmalkov

1 answer 1

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

  • elegantly turned out! - MaxU
  • Thank you, really concise and beautiful, just what I wanted. - Edward Izmalkov
  • another awful way x = [a if a> 0 else 0 for a in [3 * y + z]] [0] - vadim vaduxa