An assignment statement, e.g. x = y
, is not an expression in Python. So while chained assignments are allowed, e.g. a = b = c
, embedded assignments using the =
operator or not.
That having been said, Python 3.8 introduced the "walrus operator", :=
, which allows assignment expressions. So in Python 3.8 and later, you can do:
numTwo = numOne + numTwo - (numOne := numTwo)
to achieve the desired result. You can read about the walrus operator here and, in more detail, here.
But in Python, the normal way to swap two variables a
and b
is:
a, b = b, a
That's all that's needed.
Also note that none of the semicolons in the posted code are necessary, and would normally not be present.