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

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 29 June

    Get Day Name from Date in Python

    Table of ContentsUse the Calendar Module to Get the Name of the Day in PythonUse the strftime() Method to Get the Name of the Day in PythonUse Pandas Timestamp Method to Get the Name of the Day in PythonUse the weekday() Method to Get the Name of the Day in PythonUse the isoweekday() Method to […]

  • 27 June

    Fill Array with Random Numbers in Python

    Table of ContentsFill array with random numbers in PythonWays to fill array with random numbers in PythonUsing the numpy.random.randint() function to fill array with random numbers in PythonUsing the numpy.random.Generator.integers() function to fill array with random numbers in PythonUsing the random.randint() function to fill array with random numbers in PythonConclusionWas this post helpful? Fill array […]

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.