+= in Python (A Simple Illustrated Guide)

+= in Python

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:

Output:

Result: 15

Use Cases

1️⃣ Increment a Numerical Value

The += operator can be used to increment the value of a variable by the desired amount.

Example:

Output:

Original Value: 400
Incremented Value: 500

2️⃣Concatenate a String

The += operator allows you to join/concatenate the string held by a variable to a new string.

Example:

Output:

Original string: Python
New String: Python is Fun!

3️⃣ Append To A List

You can use += operator to append a value or an object to a list.

Example:

Output:

Original list: [‘A’, ‘B’, ‘C’, ‘D’]
New list: [‘A’, ‘B’, ‘C’, ‘D’, ‘E’]

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:

Output:

num: 25
li: [1, 2, 3, 4, 5]

+= 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:

Output:

li_1: [10, 20, 30, 40]
copy_li_1: [10, 20, 30, 40]
li_2: [10, 20, 30, 40]
copy_li_2: [10, 20, 30]

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:

Output:

num: 15

*= 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:

Output:

num: 100

/= 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:

Output:

num: 4.0

%= 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:

Output:

num: 2

//= 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:

Output:

num: 6

**= 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:

Output:

num: 8000

&= 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:

Output:

num: {‘C’, ‘E’}

|= 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:

Output:

num: {‘C’, ‘M’, ‘A’, ‘Q’, ‘B’, ‘L’, ‘E’, ‘D’}

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.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.