When writing a program in Python, there's a good chance you will need to add two values and save the resulting value in a variable at some point.
Doing this is pretty straightforward, even if you don't have much experience programming. But the nice thing is that Python provides an operator to accomplish this quickly.
In this brief tutorial, we'll discuss what this operator is and illustrate how you can use it to add values and assign the result with examples.
A Brief Refresher on Operators
Operators are symbols in Python that represent a pre-defined operation in the language. The first thing that will likely strike you is the plus sign, which represents addition. Of course, there are several operators in Python.
Let's say you want to keep a running total of a couple of numbers. Here's a simple program you could write:
runningTotal = 10 # Assigning initial value runningTotal = runningTotal + 7.5 print(runningTotal)
In the first line, we assigned 10 to the runningTotal variable.
Then, we added 7.5 to the value in runningTotal and saved this sum in runningTotal.
Finally, we printed the value of runningTotal.
As you can guess, this program would print 17.5.
Now, let's look at how the += operator can make this program easier to write.
The Python += Operator: Explanation and Examples
The += operator is a pre-defined operator that adds two values and assigns the sum to a variable. For this reason, it's termed the "addition assignment" operator.
The operator is typically used to store sums of numbers in counter variables to keep track of the frequency of repetitions of a specific operation.
Like any other operator, this one also has syntax associated with it. Here's how you'll need to use it so it works correctly:
exampleVariable += someValue
It's important to note that the variable you set needs to either be a number or a string. The number may be an integer or a floating-point number.
Let's say you initially assign a string value to the variable. Then, the value you write after the equals sign will be appended to the end of the string.
On the other hand, if a number is initially assigned to a variable, the number after the equals sign will be added to the initially assigned number.
Let's look at an example of both cases:
Numerical Example
Let's rewrite the program we discussed at the beginning of this post. Here's how you would use the addition assignment operator in the program:
runningTotal = 10 # Assigning initial value runningTotal += 7.5 print(runningTotal)
Running this code will give you the same result as the program we wrote earlier.
First, the value 10 will be assigned to runningTotal. The second statement will then add 7.5 to runningTotal.
Finally, the runningTotal value will be printed, and you will see 17.5 appear in the console.
Using this operator instead of basing the code on the basic logic used in the previous program makes it more "Pythonic." Also, the program is much easier to read and quicker to write.
String Example
Let's say we define a variable that stores the user's first name. The value of this variable is Chloe.
Then, we define a second variable to hold the user's surname. The value of this variable is Reid.
The code would look like this:
forename = "Chloe" surname = " Reid"
We now want to add these names, or rather, append them and store it as one value. We can use the addition operator to do this.
As you can see, the surname has a space at the beginning. We do this because the addition operator will not add a space to the stored string.
To merge the two names, we'd use this code:
forename += surname
The code will return the value "Chloe Reid."
Associativity of the += Operator
Let's use an example to determine the associativity of the addition assignment operator.
A = 5 B = 10 A += B>>1 print(A)
As you can see, variables A and B are assigned values 5 and 10. Then, the value of B is shifted to the right by one bit. The bit-shifted result is then added to A and stored in A.
The final statement prints the A value, showing that it equals 10. So, the associativity property of the addition assignment operator is from left to right.
Other Assignment Operators You Should Know About
Besides the addition assignment operator, Python offers a variety of other assignment operators. Some of the most interesting ones include comparison operators, membership operators, and identity operators.
Assignment operators allow you to perform mathematical functions and assign the results to a variable. Here's a list of what are considered the "main" assignment operators in Python:
Operator |
Type |
Example |
Equivalent |
+= |
Addition |
val += 2 |
val = val + 2 |
/= |
Division |
val /= 2 |
val = val / 2 |
= |
Equals |
val = 2 |
val = 2 |
*= |
Multiplication |
val *= 2 |
val = val * 2 |
** |
Power |
val ** 2 |
val = val ** 2 |
-= |
Subtraction |
val -= 2 |
val = val – 2 |
Conclusion
With this guide handy, you're prepared to use the addition assignment operator to its full potential. You can also experiment with the other operators mentioned in the table above.
You're now one step closer to mastering Python. You might have a long way to go, but as long as you continue learning, experimenting, and repeating, you will keep making progress and becoming a better programmer.