Division in Python
In this tutorial, we will understand what mathematical operation division is and how is it performed in Python Programming language. We will also focus on the operators being used in both the versions of python which are python 2 and python 3.
What is Mathematical Division
The division is a technique of allotting a cluster of belongings into equivalent shares. It is one of the four elementary mathematical processes, which gives an impartial result of the distribution. It takes two operands as input - one for the dividend (the no. that has to be divided) and the other for the divisor (one which divides).
The division works opposite to that of multiplication.
In multiplication, the resultant is 16 If 4 clusters of 4 are formed;
On the contrary, in the division, the resultant is 4 when 16 is divided into 4 equal clusters.
Example 1: 16 % 4 = 4
Here, 16 is the dividend
4 is divisor
4 is quotient
Example 2: 25 % 4 = 6 (rem=1)
Here, 25 is the dividend
4 is divisor
6 is quotient
1 is remainder
Python division
Python provides two ways to perform python division.
1.Integer Division
2.Float division
Let us try to understand both ways with the help of examples.
Integer division
Integer division gives out only the integer number.
Integer division gives out the base or floor of the division. That is the values until the decimal point gets encountered are considered and the rest are discarded.
The operator used for such operation is '//' in Python 3.
Illustration 1
# Integer division in Python 3
print (16//4)
output for the above = 4

Illustration 2
# Integer division in Python 3
print (25//5)
output for the above = 5

Python 2 does not support the ‘//’ operator.
It supports the ‘/ ‘operator for integer division.
Illustration-
# Integer division in python 2 using the '/ ' operator
print 16/4
output for the above = 4

To perform integer division using the ‘// ‘operator, one needs to import from a future module. It can be imported as-
Illustration-
# Integer division in python 2 using the '//'operator
from __future__ import division
print 16 // 4
Output for the above = 4

Float Division
In Float division, the resultant is a floating-point estimate of the result of a division.
Output depends on the capacity of a float data type. The exact result is shown until the capacity of float gets exhausted. After that number, an approximate result is shown on the output screen.
Thus, it’s not possible to store every value after the decimal point. so, it is impossible to store an exact binary representation of various floating-point numbers. This can be considered a drawback of float division as it often leads to glitches when comparing numbers or when rounding.
The operator used by float division is ‘ / ‘
In Python 3,
# Illustration 1 to show float division in python 3
print ( 20 / 6 )
output of the above code = 3.33333333333

# Illustration 2 to show float division in python 3
print ( 35 / 6 )
output for the above = 5.83333333333

In Python 2,
The only typical division operator is '/'.
If both values are integers, the resultant value or quotient is also an integer.
# Illustration 1 to show floating point division using int
print 4 / 2
output = 2

The above code gives out the integer value and not the float value.
# Illustration 2 to show floating point division using int
print 16 / 3
output = 5

The output of the above code should be 5.333333333333333 but instead, the output gives out is 3 only which is the integer value and the float value has been discarded.
To obtain a floating-point value, either of the values should be a float,
# Floating point division using one float value.
print 20 / 3.0
output = 6.666666666666667

The __future__ module can also be taken into account in python 2 to perform python division normally as python 3 does.
# Floating point division using future module
from __future__ import division
print 20 / 3
output = 6.666666666666667

In this way, we can escape from the compulsion of adding a 0 after a decimal point and perform normal float division just as it is performed in python 3.