Following this logic :
a,b = 0,0
I was expecting this to work :
a,b = 1,1
SyntaxError: illegal expression for augmented assignment
so here i am.
Is there anyway to achieve this in one line ?
CodePudding user response:
Think of it like a list of "how to update each variable" (variables to the left, formulas to the right).
a, b = a 1, b 1
CodePudding user response:
If you want it in 1 line here is what you can do.. a,b=0 is not the right way , it should have been a=b=0 or a,b=0,0
a=b=0
a =1;b =1
CodePudding user response:
You can use map to apply your increment or other operation
a,b=0,0
a, b = map( lambda x : x 1, [a,b])
output
1,1
CodePudding user response:
As already mentioned by others, you could combine two statements into one line as a = 1; b = 1.
But, if you prefer a single statement in the same "spirit" as a, b = 0, 0 then try this:
a, b = a 1, b 1
The way these assignments work is that a list of variables is assigned a list of values:
| variable | value |
|---|---|
| a | 0 |
| b | 0 |
→ a = 0; b = 0 → a, b = 0, 0
| variable | value |
|---|---|
| a | a 1 |
| b | b 1 |
→ a = a 1; b = b 1 → a, b = a 1, b 1
You can either use multiple statements, one per table row, or a single statement where all the values in the left column go on one side of the = and all the values in the right column go on the other side.
This works only for = though. Your idea a, b = 1 firstly wouldn't work for the same reason a, b = 0 doesn't (there is only one right-hand side value), but a, b = 1, 1 unfortunately also doesn't work, just because Python doesn't support this concept. with tuples would concatenate them into a larger tuple, not add each of their elements ((1, 2) (3, 4) is (1, 2, 3, 4) and not (4, 6)).
