The Python Addition (+) Operator is part of the 4 basic Arithmetic Operators found in Python. The Addition Operator is used in Python to perform mathematical operations together with the Subtraction (-), Multiplication (*) and Division (/) operators.
There are also 3 more Advanced Arithmetic Operators known as Modulus (%), Exponentiation (**) and Floor Division (//), making for a total of 7 Arithmetic Operators in Python.
The Arithmetic Operators form part of the other Python Operators collection, which includes the Assignment Operators, Comparison Operators, Logical Operators, Identity Operators, Membership Operators and Bitwise Operators.
NOTE: This tutorial requires that Python is already installed, as well as an IDE (Integrated Development Environment), such as PyCharm. It’s also recommended that you have a basic understanding of How to Use Python if this is your first time learning Python.
With that said, let’s get started with this free Python Addition Operator tutorial.
How to use the Addition Operator in Python
Let’s use the Addition Arithmetic Operator (+) to type in a basic addition equation such as “1+2” in the Python IDE.
If you “run” this code, you will see that nothing happens in the output window.
1+2
This is because Python requires a statement in order for it to know that it needs to output this equation.
The most common Statement is the Print Statement “print()”, so let’s use that and run the script again:
print(1 + 2) # “1” and “2” are the “Operands”, while “+” is the “Operator”
#>> 3 # “3” is the “Output”
Now you will see that Python outputs the value of “3“, which is the total of “1+2“, meaning that we just created some basic math in Python using the Addition Arithmetic Operator, which is used to find the sum (aka addition) of two or more values.
In this example:
- “1” and “2” are the Operands.
- “+” is the Operator.
- “3” is the Output.
Addition Operator using Float values in Python
The above example is done using Whole Numbers, which in Python is referred to as an Integer (int) Data Type.
We can however also make use of Fractional or Decimal values, which in Python are referred to as a Floating Point (float) Data Type.
Let’s now add 3.75 (float) + 2.88 (float) using the Addition Arithmetic Operator:
print(3.75 + 2.88) # float + float values
#>> 6.63 # outputs float value
Python outputs a value of “6.63“, because in math, “3.75 + 2.88 = 6.53“. As this output value is a decimal, it remains a Float Value in Python.
Arithmetic Float values are sometimes subject to a floating point errors. This is not the fault of Python but how computer binary works in general.
To illustrate this, let’s take a look as this seemingly very basic example of where we now add 0.1 (float) to 0.2 (float) using the Addition Arithmetic Operator:
print(0.1 + 0.2) # float + float values
#>> 0.30000000000000004 # outputs float value
You will quickly notice the problem here, as “0.1 + 0.2” should equal “0.3” and not “0.30000000000000004”.
To solve this Floating Point recurring issue, we can make use of the round() Function or the decimal.Decimal module.
Using the Round() Function to limit Addition decimal value to 1
While technically out of the scope of this basic introduction to the Addition Operator, if you do want to round the output off to 1 decimal value, it is possible to do so using the round() Function and setting the amount of decimal places, by entering a “, 1” after the sum as follows:
print(round(0.1 + 0.2, 1)) # round() Function with a decimal limit of 1
#>> 0.3 # outputs a rounded off float value
Now we get a much cleaner, one decimal output of “0.3” and not “0.30000000000000004”, thanks to the round() Function.
As this output value is a decimal, it remains a Float Value in Python, regardless of whether we use the round function or not.
Using the decimal.Decimal module to calculate precise Floating Point numbers
In order to avoid Floating Point values such as “0.1 + 0.2” to equal “0.30000000000000004”, we can make use of Python’s decimal.Decimal module to accurately calculate it to be “0.3”.
from decimal import Decimal
print(Decimal("0.1") + Decimal("0.2"))
#>> 0.3
Addition Operator using Float and Int values in Python
We can then also mix both int and float values as we please in Python, when using the Addition Operator:
print(3.75 + 204) # float + int value
#>> 207.75 # outputs float value
Python outputs a value of 207.75, which is again a Float Value.
When using the Addition Arithmetic Operator, to add a float value with an int value, a float value will always be the output result.
Addition Operator using Multiple Float and Int values in Python
Multiple values of both int and float values can be used with the Python Addition Operator, in any combination:
print(3.75 + 204 + 64.252 + 5 + 78) # float + int + float + int + int
#>> 355.002 # outputs float value
Here again we see the output of a float (decimal) value. This is will always be the result when using the Addition Operator within a combination of multiple float and int values, even if the output is a Whole Number
print(3.75 + 204 + 64.25 + 5 + 78) # float + int + float + int + int
#>> 355.0 # outputs float value
Even though the mathematical sum of “3.75 + 204 + 64.25 + 5 + 78 = 355”, (making 355 is a whole number), Python sees it as 355.0, which is a decimal value and therefore a Float.
Python Expressions
All the above examples of using the Addition Operator are better known as Python Expressions, which is a combination of Operators and Operands (Integer or Floating Point numbers), which are then interpreted to produce an output value.
The Addition Operator can also be combined with the other Arithmetic Operators found in Python, to form an Arithmetic Expression. When this happens, an Expression is evaluated as per the Precedence of its operators.
print(-5 + -3 * 7 + 4)
#>> -22
Here the Multiplication Operator is given presence before the Addition operator.
Conclusion
I hope you enjoyed this tutorial on the Addition Arithmetic Operator in Python. This should give you the starting knowledge you need to easily implement it into your next Python code or project.
Python Addition Operator Summary
Here is a summary of what we learnt about the Python Addition operator:
- How to use the Addition Operator with Integer numbers in Python.
- How to use the Addition Operator with Floating Point numbers in Python.
- When using Floating Point values with the Addition operator in Python, you will sometimes get recurring decimal values such as “0.1 + 0.2 = 0.30000000000000004”, which can be solved using the round() Function or the decimal.Decimal module.
- How to use the Addition Operator with a mix of both Integer and Floating Point numbers in Python.
- When using the Addition Operator with an Operand such as an Integer or Floating Point value, you are creating an Expression in Python. The addition Operator can also be combined with the other Python Arithmetic Operator in an Expression, which is then calculated based on Precedence.
Share the love
If you enjoyed this tutorial content and would like others to benefit from it as well, make sure to share it on your favorite Social Media platform, using the share buttons below.
Subscribe for News & Discount Offers
Subscribe right now to gain instant access to the latest news and tutorials while also being the first to know about, exclusive offers and discounts!