Arithmetic Operators in Python | Python Arithmetic Operator with Example
What are Arithmetic Operators in Python
An arithmetic operators in python are used to perform mathematical calculation between two operands like addition, subtraction, multiplication etc.
There are 7 types of arithmetic operators in Python:
- Addition
- Subtraction
- Multiplication
- Division
- Modulus
- Exponent
- Floor Division
All Arithmetic Operators in Python with Syntax
Symbol | Operation | Example |
---|---|---|
+ | Addition | x + y |
- | Subtraction | x - y |
* | Multiplication | x * y |
/ | Division | x / y |
% | Modulus | x % y |
** | Exponent | x ** y |
// | Floor Division | x // y |
Let's understand each operator one by one with the help of examples and know how to use arithmetic operators in Python:
Addition Operator
Addition operator in Python is used to perform addition between two operands or values. It is used to find the sum of two values.
It is represented by "+".
Example Program
a=10
b=5
c=a+b
print(c)
Output:
15
In the above program the values of variable "a" and "b" are added using the "+" operator and the result is saved in third variable "c" and finally the result is printed on the screen using the print statement.
Subtraction Operator
Subtraction operator in Python is used to perform subtraction between two operands or values.
It is represented by "-".
Example Program
a=10
b=5
c=a-b
print(c)
Output:
5
In the above program the values stored in variable "b" is subtracted from the value stored in variable "a" using the "-" operator and the result is saved in third variable "c" and finally the result is printed on the screen using the print statement.
Multiplication Operator
Multiplication operator in Python is used to perform multiplication between two operands or values. It is used to find the product of two values.
It is represented by "*".
Example Program
a=10
b=5
c=a*b
print(c)
Output:
50
In the above program the values of variable "a" and "b" is multiplied using the "*" operator and the product of the operand is assigned to the third variable "c" whose result is printed on the screen using the print statement.
Division Operator
It is represented by "/".
Example Program
a=10
b=5
c=a/b
print(c)
Output:
2
In the above example, the value of first operand that is "a" is divided by the value of second operand i.e "b" using the "/" operator.Modulus Operator
It is represented by "%".
Example Program
a=10
b=5
c=a%b
print(c)
Output:
0
In general mathematics, when we divide two number then we get two things one is quotient and another is remainder. Similarly, in the above program when we find the modulus of two operands i.e a and b using the % operator then we get remainder as a result.
Exponent Operator
Exponent operator in Python is used to raise power of one operand to the other.
It is represented by "**".
Example Program
a=10
b=2
c=10**2
print(c)
Output:
100
In given program, the value of b is raised to the power of a i.e 2 is raised to the power of 10. (10^2).
Floor Division Operator
Floor division operator in Python is use to perform division operation between two operands and get the result in round figures.
It is represnted by "".
Example Program
a=7
b=2
c=a//b
print(c)
Output:
3
In the above program, the value stored in first operand is divided by second operand and if the value after division is in decimal then all the digit after decimal is removed and number before decimal point is printed.