In Python, users can use different operators to execute operations on variables and values. These are nothing but symbols that perform operations, like logical, arithmetic, bitwise, etc. This article will cover one of the Python assignment operators (+=) and its uses.
What are Operators in Python?
Operators are symbols that act upon operands when users call them. In other words, they are the constructs that can operate the value of operands. Python includes seven types of operators. These are as follows:
- Arithmetic operators
- Logical operators
- Bitwise operators
- Assignment operators
- Comparison operators
- Identity operators
- Membership operators
What is an assignment operator?
Python includes assignment operators that allow users to allocate expressions or values (as operands) to the left-hand side. Users can represent the assignment operators with the "=" symbol. It assigns the value of the right-hand side expression to the left-hand side operand.
Users use these operators in the assignment expressions and statements.
In this article, you will learn the += operator, one of the most commonly used assignment operators.
The Python += operator, also called add and assign operator or addition assignment operator, adds a right-hand side operand to the left-hand side operand. It then assigns the resultant value to the left-hand side operand. Users generally use this assignment operator to add values to the counter variable to track the number of times something has occurred.
variable_name += value
Users must specify the value of the variable as a number or string. Also, users can use either a floating-point number or an integer as the value of the variable.
Let us see some examples of the addition assignment operator and know how it works:
a = 10 b = 20 a = a + b print (a)
The above code snippet shows how to add values of two variables with the addition operator. The operator first adds the values of the two variables and then stores the final value within the first variable.
We can also write the above code as follows:
variable_1 = 5 variable_1 += 10.5 print(variable_1)
We have taken numbers as variable inputs, and the addition assignment operator will add and assign the second number with the first value of the variable.
First, users should declare a Python variable. Here, we are using variable_1. This variable will store the value 5. Then, we use the operator +=, assign 10.5 and add it to the first value 7.5 of the variable_1 variable. Finally, we print out the value of variable_1 to the console, which returns 15.5.
Addition assignment to add and assign strings:
We can use the same operator to add two or more strings inputs. Let us see the example and learn how it works:
a = "Hello" b = " World" a = a + b print (a)
We have used the same addition operator with two different variables. The first variable has the value "Hello" and the second has "World." The operator joins the two values and assigns them to the first variable.
We can also write the above code as follows:
first_name = "Hey," second_name = "Python" first_name += second_name print (first_name)
We have declared a variable with the first string. The first variable stores "Hey," and the second variable stores "Python."
Difference between addition assignment operator += and addition operator (+):
|S. No.||Addition Assignment Operator (+=)||Addition Operator (+)|
|1||The addition assignment operator increases the efficiency of the Python code.||The addition operator is less efficient compared to the += operator.|
|2||The addition assignment operator (+=) modifies the existing list.||The addition operator (+) creates a new list.|
|3||One of the most highlighted disadvantages of the += operator is that users cannot use more than two variables and have to assign the result within a single variable. Let us take the example a += b, where the variable "a" stores the value generated by the addition operator.||With the addition operator, users can assign and declare two or three variables which is an advantage of the addition operator over the += operator. For example, let us take a = b + c, where the variable "a" stores the addition result of b and c.|
|4||The addition assignment operator performs slightly faster because users need only two variables to declare and assign values.||The addition operator is negligibly slower than the += operator as it uses and assigns values with more variables.|
We hope this article has catered an explanatory idea on how the += operators work. The addition assignment (+=) operator is one example of the Python assignment operator. Now a user can equip with the knowledge they need to use the addition assignment operator like a professional Python developer. This tutorial also discussed a few basic code examples of the operator, their explanations, and how to use the += assignment operator.