I need to know what +=
does in Python. It's that simple. I also would appreciate links to definitions of other shorthand tools in Python.

- 119,623
- 25
- 170
- 301

- 1,785
- 2
- 11
- 4
-
10[`object.__iadd__`](http://docs.python.org/reference/datamodel.html#object.__iadd__) – ephemient Jan 30 '11 at 06:06
-
1possible duplicate of [What does plus equals (+=) do in Python?](http://stackoverflow.com/questions/2347265/what-does-plus-equals-do-in-python) – AndiDog Jan 30 '11 at 08:22
-
3@AndiDog While it's true both questions are about the (+=) operator, the one you linked is about a sophisticated usage and subtle problem, and the OP here is probably not able to follow the reasoning there (yet). – Dr. belisarius Jan 30 '11 at 09:42
-
@belisarius: I was taking the question literally - it contains the word *"exactly"*. That's why I suggested the other question. I think it's totally clear what `+=` does, but not how it works under the hood. – AndiDog Jan 30 '11 at 09:45
-
3@AndiDog Perhaps you were right at that time, but looking at the (almost) accepted solutions here, is clear that this question is about a basic understanding of the operator :D – Dr. belisarius Jan 30 '11 at 09:48
-
2Most sumbol uses are now indexed in the Symbols page https://docs.python.org/3/genindex-Symbols.html. – Terry Jan Reedy Oct 31 '14 at 22:04
17 Answers
In Python, +=
is sugar coating for the __iadd__
special method, or __add__
or __radd__
if __iadd__
isn't present. The __iadd__
method of a class can do anything it wants. The list object implements it and uses it to iterate over an iterable object appending each element to itself in the same way that the list's extend method does.
Here's a simple custom class that implements the __iadd__
special method. You initialize the object with an int, then can use the +=
operator to add a number. I've added a print statement in __iadd__
to show that it gets called. Also, __iadd__
is expected to return an object, so I returned the addition of itself plus the other number which makes sense in this case.
>>> class Adder(object):
def __init__(self, num=0):
self.num = num
def __iadd__(self, other):
print 'in __iadd__', other
self.num = self.num + other
return self.num
>>> a = Adder(2)
>>> a += 3
in __iadd__ 3
>>> a
5
-
34While this is not what the Asker was looking for, +1 for the real answer. =) – Michael Feb 18 '14 at 19:35
-
-
4+1 for answering the question, but -1 for an `__iadd__` that returns a different type (which itself is addable) – Caleth Jun 01 '18 at 16:07
-
3This answer is too complex for the type of person who would need to ask what += means (i.e., a beginner). Your answer is not a beginner answer, not just because beginners usually don't start learning Python in an object-oriented way, but also because there are much simpler answers (like @Imran's below). Just my two cents, even though I appreciate this answer. – q-compute Sep 10 '19 at 15:18
-
This is not quite correct, `__iadd__` only does half of what `+=` does. Docs: "the computation and assignment are performed in two separate steps. The in-place functions listed below only do the first step, calling the in-place method. The second step, assignment, is not handled." https://docs.python.org/3/library/operator.html Section: In-place Operators. – Marcel Besixdouze Apr 13 '21 at 16:56
-
1@q-compute On the contrary, I think the only legitimate reason to look to StackOverflow for information about what `+=` does in Python would be because you've been tripped up by some of its arcane vagaries and complexities. – Marcel Besixdouze Apr 13 '21 at 17:17
+=
adds another value with the variable's value and assigns the new value to the variable.
>>> x = 3
>>> x += 2
>>> print x
5
-=
, *=
, /=
does similar for subtraction, multiplication and division.

- 87,203
- 23
- 98
- 131
x += 5
is not exactly the same as saying x = x + 5
in Python.
Note here:
In [1]: x = [2, 3, 4]
In [2]: y = x
In [3]: x += 7, 8, 9
In [4]: x
Out[4]: [2, 3, 4, 7, 8, 9]
In [5]: y
Out[5]: [2, 3, 4, 7, 8, 9]
In [6]: x += [44, 55]
In [7]: x
Out[7]: [2, 3, 4, 7, 8, 9, 44, 55]
In [8]: y
Out[8]: [2, 3, 4, 7, 8, 9, 44, 55]
In [9]: x = x + [33, 22]
In [10]: x
Out[10]: [2, 3, 4, 7, 8, 9, 44, 55, 33, 22]
In [11]: y
Out[11]: [2, 3, 4, 7, 8, 9, 44, 55]
See for reference: Why does += behave unexpectedly on lists?

- 28,235
- 9
- 60
- 81

- 3,078
- 2
- 27
- 50
-
-
Also, one of the linked threads provides a good discussion on where exactly it differs. https://stackoverflow.com/questions/6951792/python-a-b-not-the-same-as-a-a-b?noredirect=1&lq=1 – Ufos Oct 30 '19 at 11:23
+=
adds a number to a variable, changing the variable itself in the process (whereas +
would not). Similar to this, there are the following that also modifies the variable:
-=
, subtracts a value from variable, setting the variable to the result*=
, multiplies the variable and a value, making the outcome the variable/=
, divides the variable by the value, making the outcome the variable%=
, performs modulus on the variable, with the variable then being set to the result of it
There may be others. I am not a Python programmer.

- 106,965
- 23
- 235
- 261
-
2For numbers, this answer is correct. (See [Bryan's answer](https://stackoverflow.com/a/4845327/241211) for special behavior.) There [are indeed](https://docs.python.org/3.1/library/operator.html#operator.iadd) several others, including bitwise operators (`&=`, `>>=`, etc.) and additional math operators (`**=`, etc.). – Michael Dec 21 '17 at 16:12
It is not mere a syntactic sugar. Try this:
x = [] # empty list
x += "something" # iterates over the string and appends to list
print(x) # ['s', 'o', 'm', 'e', 't', 'h', 'i', 'n', 'g']
versus
x = [] # empty list
x = x + "something" # TypeError: can only concatenate list (not "str") to list
The +=
operator invokes the __iadd__()
list method, while +
one invokes the __add__()
one. They do different things with lists.
-
I was so confused about this! Thanks for your code and explanation. It looks like += only works safely for numbers. Am I right? – user3512680 Feb 18 '21 at 16:50
It adds the right operand to the left. x += 2
means x = x + 2
It can also add elements to a list -- see this SO thread.

- 1
- 1

- 51,193
- 8
- 108
- 113
Notionally a += b "adds" b to a storing the result in a. This simplistic description would describe the += operator in many languages.
However the simplistic description raises a couple of questions.
- What exactly do we mean by "adding"?
- What exactly do we mean by "storing the result in a"? python variables don't store values directly they store references to objects.
In python the answers to both of these questions depend on the data type of a.
So what exactly does "adding" mean?
- For numbers it means numeric addition.
- For lists, tuples, strings etc it means concatenation.
Note that for lists += is more flexible than +, the + operator on a list requires another list, but the += operator will accept any iterable.
So what does "storing the value in a" mean?
If the object is mutable then it is encouraged (but not required) to perform the modification in-place. So a points to the same object it did before but that object now has different content.
If the object is immutable then it obviously can't perform the modification in-place. Some mutable objects may also not have an implementation of an in-place "add" operation . In this case the variable "a" will be updated to point to a new object containing the result of an addition operation.
Technically this is implemented by looking for __IADD__
first, if that is not implemented then __ADD__
is tried and finally __RADD__
.
Care is required when using += in python on variables where we are not certain of the exact type and in particular where we are not certain if the type is mutable or not. For example consider the following code.
def dostuff(a):
b = a
a += (3,4)
print(repr(a)+' '+repr(b))
dostuff((1,2))
dostuff([1,2])
When we invoke dostuff with a tuple then the tuple is copied as part of the += operation and so b is unaffected. However when we invoke it with a list the list is modified in place, so both a and b are affected.
In python 3, similar behaviour is observed with the "bytes" and "bytearray" types.
Finally note that reassignment happens even if the object is not replaced. This doesn't matter much if the left hand side is simply a variable but it can cause confusing behaviour when you have an immutable collection referring to mutable collections for example:
a = ([1,2],[3,4])
a[0] += [5]
In this case [5] will successfully be added to the list referred to by a[0] but then afterwards an exception will be raised when the code tries and fails to reassign a[0].

- 9,724
- 2
- 38
- 51
Note x += y
is not the same as x = x + y
in some situations where an additional operator is included because of the operator precedence combined with the fact that the right hand side is always evaluated first, e.g.
>>> x = 2
>>> x += 2 and 1
>>> x
3
>>> x = 2
>>> x = x + 2 and 1
>>> x
1
Note the first case expand to:
>>> x = 2
>>> x = x + (2 and 1)
>>> x
3
You are more likely to encounter this in the 'real world' with other operators, e.g.
x *= 2 + 1
== x = x * (2 + 1)
!= x = x * 2 + 1

- 38,994
- 14
- 83
- 119
The short answer is +=
can be translated as "add whatever is to the right of the += to the variable on the left of the +=".
Ex. If you have a = 10
then a += 5
would be: a = a + 5
So, "a" now equal to 15.

- 5,478
- 4
- 19
- 39
-
1What does this answer contribute that hasn't already been discussed? It's a duplicate Answer... – Jan 15 '19 at 16:54
-
jdv, just trying to help. I'm a new contributor, so sorry if you think my answer was a duplicate. – Jan 17 '19 at 20:48
-
It's clear that it is a duplicate if you look at most of the other answers. It's fine to contribute, but you should strive for contributing something new (e.g., like the add vs iadd answer) or you want to take a stab at a clearer solution. But, as far as I can tell, the top-voted answers are about as clear as you can get for a basic answer. – Jan 17 '19 at 20:54
+=
is just a shortcut for writing
number = 4
number = number + 1
So instead you would write
numbers = 4
numbers += 1
Both ways are correct but example two helps you write a little less code

- 33,817
- 13
- 115
- 143

- 141
- 11
-
2The behaviour is the same on numbers but it's not the same in general. – plugwash May 10 '18 at 20:29
According to the documentation
x += y
is equivalent tox = operator.iadd(x, y)
. Another way to put it is to say thatz = operator.iadd(x, y)
is equivalent to the compound statementz = x; z += y
.
So x += 3
is the same as x = x + 3
.
x = 2
x += 3
print(x)
will output 5.
Notice that there are also

- 14,289
- 18
- 86
- 145
Let's look at the byte code that CPython generates for x += y
and x = x = y
. (Yes, this is implementation-depenent, but it gives you an idea of the language-defined semantics being implemented.)
>>> import dis
>>> dis.dis("x += y")
1 0 LOAD_NAME 0 (x)
2 LOAD_NAME 1 (y)
4 INPLACE_ADD
6 STORE_NAME 0 (x)
8 LOAD_CONST 0 (None)
10 RETURN_VALUE
>>> dis.dis("x = x + y")
1 0 LOAD_NAME 0 (x)
2 LOAD_NAME 1 (y)
4 BINARY_ADD
6 STORE_NAME 0 (x)
8 LOAD_CONST 0 (None)
10 RETURN_VALUE
The only difference between the two is the bytecode used for the operator: INPLACE_ADD
for +=
, and BINARY_ADD
for +
.
BINARY_ADD
is implemented using x.__add__
(or y.__radd__
if necessary), so x = x + y
is roughly the same as x = x.__add__(y)
. Both __add__
and __radd__
typically return new instances, without modifying either argument.
INPLACE_ADD
is implemented using x.__iadd__
. If that does not exist, then x.__add__
is used in its place. x.__iadd__
typically returns x
, so that the resulting STORE_NAME
does not change the referent of x
, though that object may have been mutated. (Indeed, the purpose of INPLACE_ADD
is to provide a way to mutate an object rather than always create a new object.)
For example, int.__iadd__
is not defined, so x += 7
when x
is an int
is the same as x = x.__add__(y)
, setting x
to a new instance of int
.
On the other hand, list.__iadd__
is defined, so x += [7]
when x
is a list
is the same as x = x.__iadd__([9])
. list.__iadd__
effectively calls extend
to add the elements of its argument to the end of x
. It's not really possible to tell by looking at the value of x
before and after the augmented assignment that x
was reassigned, because the same object was assigned to the name.

- 497,756
- 71
- 530
- 681
As others also said, the += operator is a shortcut. An example:
var = 1;
var = var + 1;
#var = 2
It could also be written like so:
var = 1;
var += 1;
#var = 2
So instead of writing the first example, you can just write the second one, which would work just fine.

- 460
- 5
- 16
Remember when you used to sum, for example 2 & 3, in your old calculator and every time you hit the =
you see 3 added to the total, the +=
does similar job. Example:
>>> orange = 2
>>> orange += 3
>>> print(orange)
5
>>> orange +=3
>>> print(orange)
8

- 2,454
- 3
- 37
- 64
I'm seeing a lot of answers that don't bring up using += with multiple integers.
One example:
x -= 1 + 3
This would be similar to:
x = x - (1 + 3)
and not:
x = (x - 1) + 3
The +=
cuts down on the redundancy in adding two objects with a given variable:
Long Version:
a = 10
a = a + 7
print(a) # result is 17
Short Version:
a = 10
a += 7
print(a) # result is 17

- 1,393
- 4
- 20
- 26

- 1
- 1
It’s basically a simplification of saying (variable) = (variable) + x For example:
num = num + 2
Is the same as:
num += 2

- 1
- 1