In this article, we will dive into a fundamental yet crucial concept in Python. We will be learning the significance and uses of the +=
operator in Python.
Description
+=
is an assignment operator in Python that adds the right side operand’s value to the left side operand and assigns the result to the left operand.
Syntax
a += b
Example:
1 2 3 4 5 6 7 8 |
a = 5 b = 10 b += a print("Result: ", b) |
Output:
Use Cases
1️⃣ Increment a Numerical Value
The +=
operator can be used to increment the value of a variable by the desired amount.
Example:
1 2 3 4 5 6 |
a = 400 print("Original Value: ", a) a += 100 print("Incremented Value: ", a) |
Output:
2️⃣Concatenate a String
The +=
operator allows you to join/concatenate the string held by a variable to a new string.
Example:
1 2 3 4 5 6 7 |
name = 'Python' print("Original string: ", name) name += ' is Fun!' print("New String: ", name) |
Output:
3️⃣ Append To A List
You can use +=
operator to append a value or an object to a list.
Example:
1 2 3 4 5 6 7 |
li_1 = ['A', 'B', 'C', 'D'] print("Original list: ", li_1) li_1 += 'E' print("New list: ", li_1) |
Output:
Some Vital Concepts
✨ x += 5 on a Numerical Value is not the same as x += 5 on a List
- For numbers, it means numeric addition.
- For lists, tuples, strings, etc., it means concatenation.
Example:
1 2 3 4 5 6 7 8 9 |
num = 20 li = [1, 2, 3, 4] num += 5 li += [5] print("num: ", num) print("li: ", li) |
Output:
✨ += behave unexpectedly on lists
Reason: +=
invokes the __iadd__
special method, and if it isn’t available, then it tries to use __add__
instead. So there’s a difference between these special methods. When the += operator operates on an object with an __iadd__
defined, the object is modified in-place. Otherwise, +=
tries to use the __add__
method and returns a new object.
Note:
- The
__iadd__
method is used for an in-place addition. It mutates the object that it operates on. - The
__add__
method returns a new object. It is used for the standard+
operator.
Thus, +=
changes the object’s value for mutable types like lists, whereas for immutable objects like tuples, strings and integers +=
returns a new object. This is exactly why +=
behave unexpectedly on lists.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
li_1 = copy_li_1 = [10, 20, 30] li_2 = copy_li_2 = [10, 20, 30] li_1 += [40] # Uses __iadd__, modifies li_1 in-place li_2 = li_2 + [40] # Uses __add__, creates new list, assigns it to li_2 # li_1 and copy_li_1 are still the same list print("li_1: ", li_1) print("copy_li_1: ", copy_li_1) # whereas only li_2 was changed print('li_2: ', li_2) print('copy_li_2: ', copy_li_2) |
Output:
Similar Operators
Just like the +=
operator there are other operators which have a similar syntax.
☞ -= in Python
- Name of Operator: Subtract AND
- Description: Used to subtract the right operand from the left operand and assign it to the left operand.
- Syntax: a-=b
Example:
1 2 3 4 5 6 |
num = 20 num -= 5 print("num: ", num) |
Output:
☞ *= in Python
- Name of Operator: Multiply AND
- Description: Used to multiply left operand and right operand and assign the value to the right operand.
- Syntax: a*=b
Example:
1 2 3 4 5 |
num = 20 num *= 5 print("num: ", num) |
Output:
☞ /= in Python
- Name of Operator: Divide AND
- Description: Used to divide the left operand left by the operand on the left and assign it to the left operand.
- Syntax: a/=b
Example:
1 2 3 4 5 6 |
num = 20 num /= 5 print("num: ", num) |
Output:
☞ %= in Python
- Name of Operator: Modulus AND
- Description: Used to find the modulus of the operand on the left when divided by the operand on the right.
- Syntax: a%=b
Example:
1 2 3 4 5 |
num = 20 num %= 3 print("num: ", num) |
Output:
☞ //= in Python
- Name of Operator: Floor AND
- Description: Used to divide the left operand by the right operand and assign the floored value to the left operand.
- Syntax: a//=b
Example:
1 2 3 4 5 |
num = 20 num //= 3 print("num: ", num) |
Output:
☞ **= in Python
- Name of Operator: Exponent AND
- Description: Used to find the result of exponentiation when the left operand is raised to the power of the operand on the right.
- Syntax: a**=b
Example:
1 2 3 4 5 |
num = 20 num **= 3 print("num: ", num) |
Output:
☞ &= in Python
- Name of Operator: Bitwise AND
- Description: Used to perform Bitwise AND on the right and left operands and assign the result to the left operand.
- Syntax: a&=b
Example:
1 2 3 4 5 6 |
num = set('ABCDE') num &= set('LMCEQ') print("num: ", num) |
Output:
☞ |= in Python
- Name of Operator: Bitwise OR
- Description: Used to perform Bitwise OR on the right and left operands and assign the result to the left operand.
- Syntax: a|=b
Example:
1 2 3 4 5 |
num = set('ABCDE') num |= set('LMCEQ') print("num: ", num) |
Output:
Some other operands include ^=, >>= and <<= ; We have not discussed each operator in detail here as that is beyond the scope of this article. However, I hope by now you have a clear idea about how each operator works.
Conclusion
We come to the end of this article, and I hope you enjoyed learning +=
in Python. Please subscribe and stay tuned for more exciting articles.