There are two primary types of division in Python:
- Float division(/): This returns a decimal value.
- Integer division(//): This returns the whole number closest to the result of the division, but always rounded towards minus infinity.
Float division ( / )
You can use the “/” operator to divide float values, which returns a floating-point number (a number with decimal points), even if both operands are integers.
If either operand is a float, the result is a float.
Visual Representation
Example
#Both operands are floats
print(5.0/2.0)
#One operand is a float, the other is an integer.
print(5/2.0)
# Even though both operands are integers, the result will be a float
print(5/2)
Output
2.5
2.5
2.5
To perform float division in Python 2, import the division feature from the __future__ module.
Integer division ( // )
The integer division denoted by “//”, also known as floor division.
It returns the quotient in which the digits after the decimal point are removed.
If one of the operands is a float, the result is a float, truncated down to the nearest whole number.
Visual Representation
Example
#Both operands are integer
print(5//2)
#One operand is an integer, the other is a float
print(5//2.0)
#Both operands are float
print(5.0//2.0)
Output
2
2.0
2.0
That’s it.