How to use conditional operations in python?

x+=5 if x+5<10 else 0

In this above example i should add 5 to x if x+5 is less than 10.

but as you can see i am calculating x+5 two times how can i do without repeating it.

like "add x to 5 if it doesnt become greater than 10 after adding"

is this possible on python? if yes then how do you do that?


From Python 3.8, there is the new "walrus" operator. :=, which allows assignments as expressions, just like the = operator behaves in C like languages.

You can rewrite your expression as:

x = y if (y:= x + 5) < 10 else x

Well you could convert this expression:

x + 5 < 10

into this:

x < 5

Then rephrase your if else one liner as:

x += 5 if x < 5 else 0