This is not possible because assignment is not an expression in Python.
Only expressions have a value in Python, which is different from JavaScript for example - where almost anything, including assignments, have a value.
You can write your program like this, however:
state = 1 if state == 4 else state + 1
This makes the value conditional on the state, not the assignment. It works in your case since the state variable is always assigned a new value.
In the general case, let's say you want to update different variables depending on the current state, you should stick to a regular if
statement. Don't overuse the ternary operator (x if C else y
) - only use it if it makes your code more readable.