Is multiple compound assignments possible in python

162 Views Asked by At
a=5
b=6
a=b+=6

The above statements when executed in python show syntax error. Why is it so? Are multiple compound assignments not possible in python?

1

There are 1 best solutions below

0
On

The short answer:

No, this is not possible in Python.

The long answer:

This is because Python doesn't support assignment as expression (i.e. assignment in Python does not return a value).

So while you may have been used to this working in C, due to common pitfalls with it, it was never a part of the Python language

...until last year when Python 3.8 introduced the Walrus operator (see docs here: https://docs.python.org/3/whatsnew/3.8.html).

However, even with this operator, the closest you can do is: a = (b := 6) - i.e. without the increment.