Augmented assignment (or compound assignment) is the name given to certain assignment operators in certain programming languages (especially those derived from C). An augmented assignment is generally used to replace a statement where an operator takes a variable as one of its arguments and then assigns the result back to the same variable. A simple example is x += 1 which is expanded to x = x + 1.
Questions tagged [augmented-assignment]
41 questions
2
votes
1 answer
Why there is no speed benefit of in-place multiplication when returning a numpy array?
I have defined two functions as a minimal working example.
In [2]: A = np.random.random(10_000_000)
In [3]: def f():
...: return A.copy() * np.pi
...:
In [4]: def g():
...: B = A.copy()
...: B *= np.pi
...: return…

abukaj
- 2,582
- 1
- 22
- 45
2
votes
2 answers
The possibility of an assignment operator concept for an object method
Let: a = 5, b = 10, and hello_world = 'Hello World'.
To my understanding: Python allows us to utilize assignment operators to prevent us from having to repeat the left operand. For example, a = a + b can be rewritten as a += b where both would…

Cole
- 1,715
- 13
- 23
2
votes
2 answers
Augmented assignment with frozenset
I just tried an augmented assignment on a frozenset, and the result surprised me:
>>> x = frozenset(['foo', 'bar', 'baz'])
>>> x
frozenset({'foo', 'baz', 'bar'})
>>> x &= {'baz', 'qux', 'quux'}
>>> x
frozenset({'baz'})
This isn't supposed to…

jfsturtz
- 397
- 4
- 13
2
votes
5 answers
Python one line if-else with different operators
I was fiddling with one line if and for statements in python and ran across the following problem:
I can make something like the following work:
state = 1 if state == 4 else 2
But I want to use = and += in the same context, something like…

ovunctuzel
- 135
- 1
- 10
1
vote
1 answer
How do the augmented assignment operators in C behave when the signedness of the operands do not match?
I have seen a similar question but mine is specifically referring to the augmented assignment operators such as +=, -=, etc. How exactly do the those operators work when I have the following?
extern signed s;
extern unsigned u;
s += u;
Will this be…

user16217248
- 3,119
- 19
- 19
- 37
1
vote
3 answers
Adding a string to a list adds each character separately
function = input('Enter function')
a = input('do you want to enter another function')
b = [function]
if a.lower() == 'yes':
while True:
function1 = input('Next Function')
b += function1
if function1 == 'quit':
…
user13551473
1
vote
1 answer
Is Reflected Augmented Addition (__iadd__) in Python?
For 'nicer' simpler code I was wondering if it were possible to perform a reserved/reflected augmented addition assignment.
[I have checked Stack Overflow for similar questions but cannot find any. So if this is a repeat my apologies, and please may…

Aldahunter
- 618
- 1
- 6
- 12
1
vote
1 answer
Why is a destructuring augmented assignment not possible?
Destructuring is possible in python:
a, b = 1, 2
Augmented assignment is also possible:
b += 1
But is there a reason destructuring augmented assignment cannot be done?:
a, b += 1, 2
> SyntaxError: illegal expression for augmented assignment
From…

Evan Benn
- 1,571
- 2
- 14
- 20
1
vote
1 answer
Regex for augmented assignment operation using positive look-ahead or look-behind
I have used the following regular expression to search and match augmented assignment operators:
AUG_ASSIGN = r'\+=|\-=|\*=|@=|/=|%=|/@=|&=|\|=|\^=|\<\<=|\>\>=|\*\*=|//='
I have a hunch that it is possible to eliminate the multiple occurrences of…

Seshadri R
- 1,192
- 14
- 24
1
vote
0 answers
Change QR code color in Real Time
I want to change QR code's color to yellow color from its original black color in Real time. As per my understanding it will be the topic of Augmented Reality which is new for me. I already searched on Google but nothing relevant I found over there.…

Ronak Joshi
- 1,553
- 2
- 20
- 43
1
vote
1 answer
a simple program in python , I am stumped
m = 0
for i in range(1,1000):
if i % 3 == 0 or i % 5 == 0:
m += m
print m
This gives 0 as answer.
Answer should be 233168.
Could the line ending in my IDE be an issue?
I am using pycharm.
EDIT: note to self - take a break. I found the…

Abhishek Dujari
- 2,343
- 33
- 43
0
votes
0 answers
Adding (concatenating) tuples to lists in Python
In Python3, let me define simple a list and tuple as follows:
In [1]: alist = [1, 2, 3]
In [2]: atuple = (4, 5, 6)
If I attempt an "add" (+) of the tuple to the list and assign that to the original list I get a TyperError:
In [3]: alist = alist +…

Lee McNally
- 1
- 1
0
votes
1 answer
Augmented assignment unexpected behavior in Python
Can someone explain to me the unexpected behavior while performing augmented assignment in for loop in Python in the following example:
the following code is doing n_epochs iterations, trying to get to the optimal value of parameters theta. The…

antelk
- 29
- 2
0
votes
2 answers
When do the Augmented Assignment operations behave as in-place operations and when do not?
I've always known that Python augmented operations always perform in-place operations. But seems like it's not true for every case. When I apply Augmented operations on integers, it's not in place.
var1 = 1234
print(id(var1))
var1 = var1…

Fahim
- 308
- 1
- 3
- 10
0
votes
1 answer
"countOut" -- while loop entries not from the list are not counted in augmented assignment Python 3.6
This simple program should count entries from the list and print how many there were as well
as counting entries that are not from the list. But for some reason it counts all entries as countIn despite if they are from the list or not... Appreciate…

Rish
- 73
- 7