Arithmetic Operators are used in Python to perform basic mathematical operations with Addition (+), Subtraction (-), Multiplication (*) and Division (/).
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 Arithmetic Operators tutorial.
What are Python Arithmetic Operators?
Arithmetic Operators help use to do basic mathematical algebra within Python, using Addition (+), Subtraction (-), Multiplication (*) and Division (/).
How to use the Python Addition Operator
Let’s use the Addition Arithmetic Operator (+) to type in a basic addition equation such as “1 + 2” in Python:
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.
For more information, check out my in depth tutorial on how to use the Python Addition Operator with Integer and Floating Point values.
How to use the Python Subtraction Operator
Let’s use the Python Subtraction Arithmetic Operator (-), to type in a basic subtraction equation such as “5-3” in the Python
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.
For more information, check out my in depth tutorial on how to use the Python Subtraction Operator with Integer and Floating Point values.
How to use the Python Multiplication Operator
Let’s use the Python Multiplication Arithmetic Operator (*), to type in a basic multiplication equation such as “5*3” in the Python:
print(5 * 3) # “5” and “3” are the “Operands”, while “*” is the “Operator”
#>> 15 # “15” is the “Output”
Python outputs a value of 15, because in math “5 x 3 = 15”, meaning that we just created some basic math in Python using the Multiplication Arithmetic Operator, which is used to find the product (aka multiplication) of two or more values.
In this example:
- “5” and “3” are the Operands
- “*” is the Operator
- “15” is the Output.
For more information, check out my in depth tutorial on how to use the Python Multiplication Operator with Integer and Floating Point values.
How to use the Python Division Operator
Let’s use the Python Division Arithmetic Operator (/), to type in a basic Division equation such as “20/4” in the Python:
print(20 / 4) # “20” and “4” are the “Operands”, while “/” is the “Operator”
#>> 5 # “5” is the “Output”
Python outputs a value of “5”, because in math “20 ÷ 4 = 5”, meaning that we just created some basic math in Python using the Division Arithmetic Operator, which is used to find the quotient (aka division) of two or more values.
In this example:
- “20” and “4” are the Operands
- “/” is the Operator
- “5” is the Output.
For more information, check out my in depth tutorial on how to use the Python Division Operator with Integer and Floating Point values.
Advanced Python Arithmetic Operators
Beyond the basic four Addition, Subtraction, Multiplication and Division operators, are also three more advanced Python operators known as the Modulus, Exponentiation and Floor Division, that allow us to compute more advanced calculations in Python, as we would in regular mathematics.
How to use the Python Modulus Operator
The Python Modulus Arithmetic Operator (%), which is used to find the remainder value, after the first operand has been divided by the second.
Note that while this uses the % symbol, it has nothing to do with percentages in Python.
print(20 % 3)
#>> 2
Python outputs a value of 2. This is calculated based on how many times 3 goes into 20 as a whole number, which in this case is 6 times (3 x 6 = 18).
Python then subtracts 18 from 20 (20 – 18 = 2) which gives us the output value of 2.
For more information, check out my in depth tutorial on how to use the Python Modulus Operator with Integer and Floating Point values.
How to use the Python Exponential Operator
Python Expressions
All the above examples are known as Python Expressions which is a combination of Operators and Operands (Integer or Floating Point numbers) that are interpreted to produce an output value.
A combination of Arithmetic Operators can be used 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)
#>> -1.4285714285714288
So that if there is more than one operator in an expression, Python sets their precedence automatically, therefore deciding which operation will be performed first.
Expression Precedence
In this example, Python will always give the multiplication Operator a higher precedence than the Addition or Subtraction operators.
It therefore first calculates “-3 x 7” = -21”, then subtracted “-5” from that to equal “-26”, before adding ”4” to that to equal “-22”
We can however take control of the precedence with the use of Parentheses (), to give an expression within an expression higher precedence.
print((-5 + -3) * 7 + 4)
#>> -52
In this case, “-5 + -3 = -8” is calculated first as it has the Parentheses () and therefore has the highest precedence. It is then multiplied by “7” to equal “-54”, before “4” is added to, to give the final output result of “-52”.
Conclusion
I hope you enjoyed this tutorial on the Arithmetic Operators in Python. This should give you the starting knowledge you need to easily implement it into your next Python code or project.
Python Arithmetic Operator Summary
Here is a summary of what we learnt about the Python Division operator:
- How to use the Division Operator with Integer numbers in Python.
- How to use the Division Operator with Floating Point numbers in Python.
- When using Floating Point values with the Division operator in Python, you will sometimes get recurring decimal values such as “10.8 / 2.4” = “4.500000000000001”, which can be solved using the round() Function or the decimal.Decimal module.
- How to use the Division Operator with a mix of both Integer and Floating Point numbers in Python.
- When using the Division Operator with an Operand such as an Integer or Floating Point value, you are creating an Expression in Python. The Division 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!