Often, when you’re performing a mathematical function in Python, you’ll want to both perform the operation, then assign the result of the operation to a variable.
There are two approaches you can use to accomplish this task, but the most efficient is to use Python’s assignment operators.
This tutorial will discuss, with reference to an example, the basics of Python operators, the += operator, and how you can use assignment operators in your code.
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.
There are several different types of operators in Python, such as logical operators and mathematical operators. For this tutorial, we’re going to focus on the += operator.
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. In our program, 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 together using a + b, and we printed out the result to the console
While this code is simple, if we wanted to add a greater amount of numbers, our code would quickly become more difficult to read. That’s why, when you want to keep a running total, the addition assignment operator is often used.
The Python += Operator
The += operator is an example of an assignment operator. Assignment operators allow you to assign a value to a variable.
This operator represents an addition assignment in Python. So, when you use the addition assignment operator, two numbers will be added together, then the resultant value will be assigned to a variable.
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 allows us to add a number to another number more efficiently in our code.
First, we declare a variable called
a which 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
In simple terms, 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.
Other Assignment Operators
The Python programming language offers a wide range of other assignment operators that can be used to 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 += operator allows you to perform an addition operation, then assign the result of the operation to a variable. The += operator is an example of a Python 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 about careers that require Python as a skill? Download the free Career Karma app today to consult with a career coach about the career paths open in Python development.