Arithmetic operators in Python can be used to perform different arithmetic calculations such as addition, subtraction, multiplication, and division. In Python, we have two additional arithmetic operators – exponent and floor division.
Here is the list of all arithmetic operators in Python along with their representation.
Operator | Name | Description |
+ | Addition | The operands on either side of this operator are added. |
– | Subtraction | The operand on the right side of this operator is subtracted from the one on the left side. |
* | Multiplication | The operands on either side of this operator are multiplied |
/ | Division | The operand on the left side of this operator is divided by the one on the right side. As a result, it returns the quotient in the form of a floating-point value. |
% | Modulo | The operand on the left side of this operator is divided by the one on the right side. As a result, it returns the remainder value. |
** | Exponent | This operator raises the left side operand to the power of the right side operand. |
// | Floor division | The operand on the left side of this operator is divided by the one on the right side. As a result, it returns the quotient in the form of an integer value. |
Program on Basic Calculations using Arithmetic Operators in Python
The above arithmetic operators can be applied to various data types like Numbers, Strings, Lists, Tuples, etc. But more precisely, all the 7 arithmetic operators can be used only with values of the Number data type (int, float, complex). We can only use selected arithmetic operators (+ and *) on a string, tuple, and list for concatenation or repetition of data.
Let’s understand this better with the below example. Let us consider two numbers 9, 2 to perform these arithmetic operations.
#Basic operations using arithmetic operators
a = 9
b = 2
#addition of two numbers
print (a+b)
#subtraction of two numbers
print (a-b)
#multiplication of two numbers
print (a*b)
#division of two numbers
print (9/2) #result will be a float value by default
#floor division of two numbers
print (9//2) #result will be an integer by default
#modulo of two numbers
print (9%2)
#exponent of two numbers
print (9**2)
Output:
11
7
18
4.5
4
1
81
Program to Compute an Expression with Several Arithmetic Operators
Consider the below program on computing a mathematical expression containing various arithmetic operators. Can you guess the output of this program?
a = 9
b = 2
#expression using arithmetic operators
c = a + b - a * b / a
print (c)
Well, this is when it gets complex. When an expression consists of multiple arithmetic operators, it’s hard to calculate the output as we are not aware of the precedence of operators. So to figure out the output of the program, let’s quickly understand arithmetic operator precedence in Python.
Precedence of Arithmetic Operators in Python
The arithmetic operator precedence makes it easy to understand which among the given operations should be computed first. Below is the table with arithmetic operator precedence in descending order.
Operator | Name |
** | Exponent |
*, /, %, // | Multiplication, Division, Modulo, Floor Division |
+, – | Addition, Subtraction |
Now, since a lot of operators have the same precedence, we use the principle of associativity. Associativity of arithmetic operators is left to right. This means, when reading the expression from left to right, the operator that appears first among the operators with equal precedence gets computed first.
So, in our above example, evaluation happens in this order
1) a*b gets evaluated first => 9*2 = 18
2) (Output of 1)/a gets evaluated next => 18/9 = 2
3) (a+b) gets evaluated next => 9+2 = 11
4) Finally, (output of 3 – output of 2) gets evaluated => 11-2 = 9
Thus, the output is 9.
Arithmetic Operators Related FAQs
Arithmetic operators are operators used to perform basic mathematical calculations like addition, subtraction, multiplication, division etc.
There are 7 arithmetic operators in Python and all of them are discussed in this article.
Addition, subtraction, division, multiplication, floor division, exponent, modulo are the 7 different operators used in Python.
Exponent (**) has the highest precedence among arithmetic operators in Python.
Yes, arithmetic operators can be used with strings in Python. The addition (+) & Multiplication (*) operators can be used for string concatenation and repetition. Other operators cannot be used with strings.
The precedence of arithmetic operators in Python is discussed in this article.