What exactly does += do in Python?

In this Python article, we will learn about += Operator and what exactly does += do in Python? But Before the lets look a quick overview of operators and types of operators in Python Programming language.

Table Of Contents

Python Operators

Operators are referred as the special symbols that perform specific operations on one or more operands. Below are the Operators Python Programming language has:

  • Arithmetic Operators: Performs basics mathematics operators. +,-*,% and / are examples.
  • Assignment operators: These operators assign the value to a variable. Like =,+=, -=, *=, /= are examples.
  • Comparison operators: These operators compare the values of the operands on the left hand side and right hand side and return a Boolean value. Like, ==, !=, >, <, >=, <= are the examples of Comparison operators.
  • Logical operators: These operators perform logical operations, such as and, or, and not.

+= Operator in Python

The += Operator in Python is a compound operator, that modifies the value of the left operand in-place, rather that creating a new value. This operator is called as Assignment Operator specifically Add and Assign. The += operator is used to add the right operand to the left operand and assign the result to the left operand. The += operator can be used with any data type that supports addition, such as integers, floats, complex numbers, and strings. The += Operator is not supported for lists, dictionaries or other mutable data types.

Using += operator with Numbers

When used with int or float values, this operator adds the value to right operator. If given a += b, this means a = a + b.
See the example code below:

CODE :

# initialized an int value.
a = 10

# initialized a float value.
b = 2.5

# Using add assign operator.
a += b

print(a)

OUTPUT :

12.5

In the above code and output you can see when used with integers or floats, this operator adds the value of left operand and right operand to the left operand, here a = a+b or a = 10 + 2.5.

using += Operators with string

The += operator can also be used with strings. This operator is used for string concatenation. The String concatenation is the process of combining two or more different strings into one string. See the example code below:

CODE :

# Initialized two strings.
name = 'Virat'
title = ' Kohli'

# using += operator for string concatenation.
name += title

print(name)

OUTPUT :

Virat Kohli

In the above code and example you can see += operator can be used for string concatenation.

Summary

In this Python article, we learned about Python Operators and Types of Operators. Also we learned about Add Assign Operator (+=) in Python and what it does. So basically, what this operator does is add the right operand and left operand and assign the value to the left operand instead of creating a new variable. Make sure to practice with the examples, in order to have a better understanding of this problem. Thanks.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top