Python Arithmetic Operators
Last Updated :
03 May, 2024
The Python operators are fundamental for performing mathematical calculations in programming languages like Python, Java, C++, and many others. Arithmetic operators are symbols used to perform mathematical operations on numerical values. In most programming languages, arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).
Arithmetic Operators in Python
There are 7 arithmetic operators in Python. The lists are given below:
Precedence of Arithmetic Operators in Python
Let us see the precedence and associativity of Python Arithmetic operators.
Operator
| Description
| Associativity
|
---|
**
| Exponentiation Operator | right-to-left
|
%, *, /, //
| Modulos, Multiplication, Division, and Floor Division | left-to-right
|
+, –
| Addition and Subtraction operators | left-to-right
|
Addition Operator
In Python, + is the addition operator. It is used to add 2 values.
Python
val1 = 2
val2 = 3
# using the addition operator
res = val1 + val2
print(res)
Output:
5
Subtraction Operator
In Python, – is the subtraction operator. It is used to subtract the second value from the first value.
Python
val1 = 2
val2 = 3
# using the subtraction operator
res = val1 - val2
print(res)
Output:
-1
Multiplication Operator
Python * operator is the multiplication operator. It is used to find the product of 2 values.
Python
val1 = 2
val2 = 3
# using the multiplication operator
res = val1 * val2
print(res)
Output :
6
Division Operator
Python // operator is the division operator. It is used to find the quotient when the first operand is divided by the second.
Python
val1 = 3
val2 = 2
# using the division operator
res = val1 / val2
print(res)
Output:
1.5
Floor Division Operator
The // in Python is used to conduct the floor division. It is used to find the floor of the quotient when the first operand is divided by the second.
Python
val1 = 3
val2 = 2
# using the floor division
res = val1 // val2
print(res)
Output:
1
Modulus Operator
The % in Python is the modulus operator. It is used to find the remainder when the first operand is divided by the second.
Python
val1 = 3
val2 = 2
# using the modulus operator
res = val1 % val2
print(res)
Output:
1
Exponentiation Operator
In Python, ** is the exponentiation operator. It is used to raise the first operand to the power of the second.
Python
val1 = 2
val2 = 3
# using the exponentiation operator
res = val1 ** val2
print(res)
Output:
8
Please Login to comment...