The Python += operator lets you add two values together and assign the resultant value to a variable. This operator is often referred to as the addition assignment operator. It is shorter than adding two numbers together and then assigning the resulting value using both a + and an = sign separately.
There may be a case where you want to add two values and assign the resulting value to a variable. That’s where Python’s addition assignment operator can come in handy.
This tutorial will discuss using the += operator to add two values and assign the final value to a variable. We’ll walk through two examples to illustrate this operator in action.
In Python, an operator is a symbol that represents a predefined operation. For instance, the plus sign (+) performs an addition operation, and the asterisk (*) performs a multiplication operation.
Suppose we want to keep a running total of two numbers in Python. We could do so using this code:
a = 10 a = a + 7.5 print(a)
Our code returns: 17.5. We assigned the value 10 to the variable a, and we assigned the value 7.5 to the variable b. Then, we added the two numbers using a + b. We printed out the result to the console.
The Python += Operator
The Python += operator adds two values together and assigns the final value to a variable. This operator is called the addition assignment operator. This operator is often used to add values to a counter variable that tracks how many times something has happened.
When you use the addition assignment operator, two numbers will be added. The resultant value will be assigned to a variable.
The syntax for this arithmetic operator is:
variable_name += value
The value of the variable you specify must be either a Python string or a number. A number can be an integer or a floating-point value.
Say you specify a string value. The value that comes after the equals sign will be added to the end of the value stored in the variable you specify. If you specify a numeric value, the two numbers will be added.
Let’s look at two examples of using to use the += operator.
+= Operator Python: Numerical Example
Suppose we want to keep a running total of a few numbers. We could do so using the += assignment operator, like this:
a = 10 a += 7.5 print(a)
Our code returns: 17.5. In our code, you can see that we have used the += operator. This operator is more “Pythonic” than the alternative. The alternative is using a plus sign to add two values and the equals sign to assign the value to a variable.
First, we declare a Python variable called a. This variable stores the value 10. Then, we use a += 7.5 to add 7.5 to the a variable. Finally, we print out the value of a to the console, which returns 17.5.
Using the addition assignment operator in this example is like writing out a = a + 7.5. The result is the same, but the addition assignment operator is easier to read.
+= Operator Python: String Example
We have a variable that stores a user’s first name. At the moment, this variable is equal to “John”. We also have a variable that stores John’s surname. The value of this variable is “Swanson”. You can see this variable below:
forename = "John" surname = " Swanson"
We want to add these two names together. Notice that the user’s surname begins with a space. This is important because the += operator does not add a space to a value. We can merge our two names together using the following code:
forename += surname
Our code returns:
We have successfully merged the contents of our two variables.
Other Python Assignment Operators
The Python programming language offers a range of other assignment operators. You can also get membership operators, comparison operators, and identify operators in Python.
Assignment operators let you perform a mathematical function, then assign the resultant value to a variable. Here are a few of the main assignment operators offered by Python:
|=||Equals||a = 2||a = 2|
|+=||Addition||a += 2||a = a + 2|
|-=||Subtraction||a -= 2||a = a – 2|
|*=||Multiplication||a *= 2||a = a * 2|
|/=||Division||a /= 2||a = a / 2|
|**||Power||a ** 2||a = a ** 2|
To learn more about how mathematical operators work in Python, read our beginner’s guide to Python math operators.
The Python += operator performs an addition operator and then assigns the result of the operation to a variable. The += operator is an example of a Python assignment operator. This operator is called the addition assignment operator.
This tutorial discussed, with reference to an example, the basics of Python operators and how to use the += assignment operator. Now you’re equipped with the knowledge you need to use the =+ operator like a professional Python developer!
Are you interested in learning more about the Python programming language? Check out our complete How to Learn Python guide with top learning resources and online courses.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot