Arithmetic Operators
Last updated
Last updated
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.
Operator | Meaning | Example |
---|---|---|
This operator is used to add two values present on either side of the operator.
Input:
Output
This operator is used to subtract the value present on the right side of the operator from the value present on the left side of the operator.
Input:
Output:
This operator is used to find the product of the two values present on either side of the operator.
Input:
Output:
This operator is used to find the quotient. The value present on the left side of the operator acts as a Dividend and the one on the right side is Divisor.
Input:
Output:
A division operation always results in a floating-point number.
This operator is used to find the remainder. The value present on the left side of the operator acts as a Dividend and the one on the right side is Divisor.
Input:
Output:
The remainder will be positive if the Dividend is positive and vice-versa. Even if the Divisor is negative but the Dividend is positive, the remainder will be positive.
This operator is used to raise the first value to the power of the second operator
Input:
Output:
The Floor Division operator is used to floor the result to the nearest integer.
Input:
Output:
Arithmetic Operators in Python follow a basic order of precedence. When more than one operator is used, they are executed according to this order:
Note: The operator listed at the top of the table will be executed first.
Input:
Output:
Here, as you can see according to the order of precedence, Parentheses will be computed first. So inside the innermost parenthesis, there is an addition operator.
Closing Thoughts on Arithmetic Operators in Python We discussed 7 different types of Arithmetic operators in Python. Make sure you remember the order of precedence because that affects the outcome of all operations performed in Python.
Operator | Purpose |
---|---|
+
Add two operands or unary plus
5 + 3 = 8
-
Subtract right operand from the left or unary minus
5 - 3 = 2
*
Multiply two operands
5 * 3 = 15
/
Divide left operand by the right one (always results into float)
5 / 3 = 1.6666666666666667
//
Integer division (results into integer)
5 // 3 = 1
%
Modulus - remainder of the division of left operand by the right
5 % 3 = 2
**
Exponentiation - left operand to the power of right operand
5 ** 3 = 125
()
Parentheses
**
Exponentiation
%, *, /, //
Modulus, Multiplication, Division, Integer Division
+, -
Addition, Subtraction