Python Arithmetic Operators are used to perform mathematical operations like addition, subtraction, multiplication, and division.
Arithmetic Operators in Python
There are 7 arithmetic operators in Python. The lists are given below:
|
Operator
|
Description
|
Syntax
|
|
+
|
Addition: adds two operands |
x + y
|
|
–
|
Subtraction: subtracts two operands |
x – y
|
|
*
|
Multiplication: multiplies two operands |
x * y
|
|
/
|
Division (float): divides the first operand by the second |
x / y
|
|
//
|
Division (floor): divides the first operand by the second |
x // y
|
|
%
|
Modulus: returns the remainder when the first operand is divided by the second |
x % y
|
|
**
|
Power (Exponent): Returns first raised to power second |
x ** y
|
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
|
Python Arithmetic Operators Examples
Addition Operator
In Python, + is the addition operator. It is used to add 2 values.
Python3
val1 = 2
val2 = 3
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.
Python3
val1 = 2
val2 = 3
res = val1 - val2
print(res)
|
Output:
-1
Multiplication Operator
In Python, * is the multiplication operator. It is used to find the product of 2 values.
Python3
val1 = 2
val2 = 3
res = val1 * val2
print(res)
|
Output :
6
Division Operator
In Python, / is the division operator. It is used to find the quotient when the first operand is divided by the second.
Python3
val1 = 3
val2 = 2
res = val1 / val2
print(res)
|
Output:
1.5
Modulus Operator
In Python, % is the modulus operator. It is used to find the remainder when the first operand is divided by the second.
Python3
val1 = 3
val2 = 2
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.
Python3
val1 = 2
val2 = 3
res = val1 ** val2
print(res)
|
Output:
8
Floor Division Operator
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.
Python3
val1 = 3
val2 = 2
res = val1 // val2
print(res)
|
Output:
1
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the
Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
09 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...