Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

+= in Python (A Simple Illustrated Guide)

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:

a = 5
b = 10
b += a
print("Result: ", b)

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:

a = 400
print("Original Value: ", a)
a += 100
print("Incremented Value: ", a)

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:

name = 'Python'
print("Original string: ", name)
name += ' is Fun!'
print("New String: ", name)

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:

li_1 = ['A', 'B', 'C', 'D']
print("Original list: ", li_1)
li_1 += 'E'
print("New list: ", li_1)

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:

num = 20
li = [1, 2, 3, 4]
num += 5
li += [5]
print("num: ", num)
print("li: ", li)

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:

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:

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:

num = 20
num -= 5
print("num: ", num)

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:

num = 20
num *= 5
print("num: ", num)

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:

num = 20
num /= 5
print("num: ", num)

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:

num = 20
num %= 3
print("num: ", num)

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:

num = 20
num //= 3
print("num: ", num)

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:

num = 20
num **= 3
print("num: ", num)

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:

num = set('ABCDE')
num &= set('LMCEQ')
print("num: ", num)

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:

num = set('ABCDE')
num |= set('LMCEQ')
print("num: ", num)

Output:

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

Some other operands include ^=, >>= and

Conclusion

We come to the end of this article, and I hope you enjoyed learning how += works in Python. Please subscribe and stay tuned for more exciting articles.



This post first appeared on How To Learn Java Programming, please read the originial post: here

Share the post

+= in Python (A Simple Illustrated Guide)

×

Subscribe to How To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×