The Python Subtraction (-) Operator is part of the 4 basic Arithmetic Operators found in Python. The Subtraction Operator is used in Python to perform mathematical operations together with the Addition (+), 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 Subtraction Operator in Python
Let’s use the Python Subtraction Arithmetic Operator (-), to type in a basic subtraction equation such as “5-3” in the Python IDE.
If you “run” this code, you will see that nothing happens in the output window.
5 - 3
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(5 - 3) # “5” and “3” are the “Operands”, while “-” is the “Operator”
#>> 2 # “2” is the “Output”
Python outputs a value of 2, because in math “5 – 3 = 2”, meaning that we just created some basic math in Python using the Subtraction Arithmetic Operator, which is used to find the difference (aka subtraction) of two or more values.
In this example:
- “5” and “3” are the Operands
- “–” is the Operator
- “2” is the Output.
Subtraction 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 subtract 2.88 (float) from 3.75 (float) using the Subtraction Arithmetic Operator.
print(3.63 - 2.88) # float - float values
#>> 0.75 # outputs float value
Python outputs a value of 0.75, because in math, “3.63 – 2.88 = 0.75”. 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 subtract 2.88 (float) from 3.75 (float) using the Subtraction Arithmetic Operator:
print(3.75 - 2.88) # float - float values
#>> 0.8700000000000001 # outputs float value
You will quickly notice the problem here, as “3.75 – 2.88” should equal “0.87” and not “0.8700000000000001”.
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 Subtraction decimal value to 2
While technically out of the scope of this basic introduction to the Subtraction Operator, if you do want to round the output off to two decimal values, it is possible using the round() Function and setting the amount of decimal places, using “, 2” after the sum:
print(round(3.75 - 2.88, 2)) # round() Function with a decimal limit of 2
#>> 0.87 # outputs a rounded off float value
Now we get a much cleaner, two decimal output of “0.87”, 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 “3.75 – 2.88” to equal “0.8700000000000001”, we can make use of Python’s decimal.Decimal module to accurately calculate it to be “0.87”.
from decimal import Decimal
print(Decimal("3.75") - Decimal("2.88"))
#>> 0.87
Subtraction 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 Subtraction Operator:
print(36.75 - 21) # float - int value
#>> 15.75 # outputs float value
Python outputs a value of 15.75, which is again a Float Value.
When using the Subtraction Arithmetic Operator, to subtract a float value with an int value, a float value will always be the output result.
Subtraction Operator using Multiple Float and Int values in Python
Multiple values of both int and float values can be used with the Python Subtraction Operator, in any combination:
print(3.75 - 204 - 64.252 - 5 - 78) # float - int - float - int - int
#>> –347.502 # outputs float value
Here again we see the output of a float (decimal) value. This is will always be the result when using the Subtraction Operator within a combination of multiple float and int values, even if the output is technically a Whole Number:
print(3.75 - 204 - 64.75 - 5 - 78) # float - int - float - int - int
#>> 348.0 # outputs float value
Even though the mathematical subtraction of “3.75 – 204 – 64.75 – 5 – 78 = 348”, (making 348 is a whole number), Python sees it as 348.0, which is a decimal value and therefore a Float.
Python Expressions
All the above examples of using the Subtraction 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 Subtraction 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 Subtraction operator.
Conclusion
I hope you enjoyed this tutorial on the Subtraction 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 Subtraction Operator Summary
Here is a summary of what we learnt about the Python Subtraction operator:
- How to use the Subtraction Operator with Integer numbers in Python.
- How to use the Subtraction Operator with Floating Point numbers in Python.
- When using Floating Point values with the Subtraction operator in Python, you will sometimes get recurring decimal values such as “3.75 – 2.88” = “0.8700000000000001”, which can be solved using the round() Function or the decimal.Decimal module.
- How to use the Subtraction Operator with a mix of both Integer and Floating Point numbers in Python.
- When using the Subtraction Operator with an Operand such as an Integer or Floating Point value, you are creating an Expression in Python. The Subtraction 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!