Python Arithmetic Operators
Python Arithmetic operators :
Python provides some arithmetic operators which perform the task of arithmetic operations. Below are some arithmetic operators -
Consider the value of num1 = 10 and num2 = 5
Operator | Explanation | Example |
---|---|---|
+ | It is used to perform addition of two numbers | num1 + num2 will give 15 |
- | It is used to perform subtraction of two numbers | num1 - num2 will give 5 |
* | It is used to perform multiplication of two numbers | num1 * num2 will give 50 |
/ | It is used to perform division of two numbers | num1 / num2 will give 2 |
% | It is used to perform modulo operator where num1 is divided by num2 and remainder will be returned | num1 % num2 will give 0 |
** | It is used to perform exponential calculation of two numbers | num1 ** num2 will give 10 to the power 5 |
// | It is used to perform floor division of two numbers | 10//2 will give 5 |
Floor Division :
In the Python floor division, when a number is divided by another number then digits after the decimal point of result will be removed.
Operator | Explanation |
---|---|
10 // 3 | Result of the operation is 3 mathematically in case of integer division. Floor division also provide result = 3 |
10.0 // 3 | Result of the operation is 3.33 mathematically but using floor division result would be 3, as it removes digits after decimal point |
Example:
#!/usr/bin/python num1 = 10 num2 = 5 res = 0 res = num1 + num2 print ("Line 1 - Result of + is ", res) res = num1 - num2 print ("Line 2 - Result of - is ", res) res = num1 * num2 print ("Line 3 - Result of * is ", res) res = num1 / num2 print ("Line 4 - Result of / is ", res) res = num1 // num2 print ("Line 5 - Result of // is ", res) res = num1 ** num2 print ("Line 6 - Result of ** is ", res) res = num1 % num2 print ("Line 7 - Result of % is ", res)
Output :
>>> Line 1 - Result of + is 15 Line 2 - Result of - is 5 Line 3 - Result of * is 50 Line 4 - Result of / is 2.0 Line 5 - Result of // is 2 Line 6 - Result of ** is 100000 Line 7 - Result of % is 0 >>>