Search code examples
pythonassignment-operator

Multiple "calculation AND assignment" operations in one line of python?


Let's say I would like to divide by 2, add 1 and multiply by 5.

Something like

x /= 2 += 1 *= 5

obviously doesn't work.

I tried different combinations using parentheses and the walrus operator, but couldn't produce produce something working, even less something well readable


Solution

  • This isn't something you can chain. There's nothing wrong with the perfectly readable

    x /= 2
    x += 1
    x *= 5
    

    or

    x = (x/2 + 1) * 5