The Python += Operator. The Python += operator adds two values together and assigns the final value to a variable. This operator is called the addition assignment operator.
In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.
-= Subtraction AssignmentSubtracts a value from the variable and assigns the result to that variable.
+= adds another value with the variable's value and assigns the new value to the variable.
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
Hope this helps.
+=
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.
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?
+=
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 itThere may be others. I am not a Python programmer.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With