26

I am quite experienced with Python, but recently, when I was looking at the solutions for the codility sample tests I encountered the operators -=, +=, ^= and I am unable to figure out what they do. Perhaps could anyone explain the context in which they are used?

2
  • 1
    I'm confused, wouldn't X += Y be essentially X = X + Y unless it's being overloaded by the library you're using?
    – Kristina
    Commented Jun 15, 2016 at 20:51
  • 1
    Ah yes that's right thank you!
    – Mc Tor
    Commented Jun 15, 2016 at 22:24

3 Answers 3

57

As almost any modern language, Python has assignment operators so they can use them every time you want to assign a value to a variable after doing some arithmetic or logical operation, both (assignment and operation) are expressed in a compact way in one statement...

Table from Tutorials Point:

Operator Description Example
= Assigns values from right side operands to left side operand c = a + b assigns value of a + b into c
+= Add AND It adds right operand to the left operand and assign the result to left operand c += a is equivalent to c = c + a
-= Subtract AND It subtracts right operand from the left operand and assign the result to left operand c -= a is equivalent to c = c - a
*= Multiply AND It multiplies right operand with the left operand and assign the result to left operand c *= a is equivalent to c = c * a
/= Divide AND It divides left operand with the right operand and assign the result to left operand c /= a is equivalent to c = c / a
%= Modulus AND It takes modulus using two operands and assign the result to left operand c %= a is equivalent to c = c % a
**= Exponent AND Performs exponential (power) calculation on operators and assign value to the left operand c **= a is equivalent to c = c ** a
//= Floor Division It performs floor division on operators and assign value to the left operand c //= a is equivalent to c = c // a
4
  • 5
    What is the meaning of all the "AND"s in the leftmost column of this table? Is it somehow supposed to mean "and assign"? It's quite confusing, because, being capitalized, might refer to the AND operator. Except Python's AND operator is lowercase. Hmmm.
    – gwideman
    Commented Jul 13, 2019 at 23:59
  • 1
    Official docs: augmented assignment statements
    – wjandrea
    Commented Nov 24, 2021 at 18:47
  • 2
    Worth noting that this table is not complete. There's also &=, among others.
    – wjandrea
    Commented Nov 24, 2021 at 19:46
  • 1
    @ΦXocę, it looked like you screenshotted the table from Tutorials Point without giving them credit, which constitutes plagiarism, so to fix it, I added a reference to the source. For more details, see referencing help and this FAQ. Also, to avoid a picture of text, I copied the original text and formatted it as a table with the help of Paste to Markdown.
    – wjandrea
    Commented Nov 24, 2021 at 20:19
3

When you compute X = X + Y you are actually returning the sum of X and Y into a new variable, which, in your example, overwrites the previous value of X. When you use an assignment operator in the form of X += 1, the value 1 is directly summed on the current value of X, without returning the result in a new variable. Take a look at the code below:

>>> V = np.arange(10)
>>> view = V[3:]        # view is just a subspace (reference) of the V array
>>> print(V);print(view)
[0 1 2 3 4 5 6 7 8 9]
[3 4 5 6 7 8 9] 
>>> view = view + 3     # add view to a constant in a new variable 
>>> print(V);print(view)
[0 1 2 3 4 5 6 7 8 9]
[ 6  7  8  9 10 11 12]
>>> view = V[3:]
>>> view += 3           # here you actually modify the value of V
>>> print(V);print(view)
[ 0  1  2  6  7  8  9 10 11 12]
[ 6  7  8  9 10 11 12]

You can also look for the documentation of numpy.ndarray.base to check if an array is actually a reference of another array.

0
x += a : x = x + a
x -= a : x = x - a

x ^= a : doesn't exist in Python. Instead use "**=". In Python we use **= instead of '^' as a power operator.

1
  • This duplicates another answer. @ΦXocę's answer already contains your response and has much more information, as well. Please don't post an answer unless you actually have something new to contribute. You can show your support for an original answer by upvoting.
    – Ben A.
    Commented Sep 7, 2023 at 17:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.