
210 views
Difference between Floor Division and Float Division in Python
The Python division can be performed in two main ways: floor division and float division. The difference lies in how the quotient is calculated and the type of result produced.
- Floor Division (
//
operator):
- Floor division is performed using the double forward slash
//
operator. - It returns the largest integer (floor) that is less than or equal to the exact mathematical result of the division.
- The result is always an integer, even if the operands are floats.
- Floor division is often used when you want to discard the fractional part of the division and obtain an integer quotient. Example:
Python
result = 7 // 3 # Result is 2
- Float Division (
/
operator):
- Float division is performed using the single forward slash
/
operator. - It returns the exact mathematical result of the division, including any fractional part.
- The result is a float, even if both operands are integers.
- Float division is used when you need a more precise result with the decimal portion included. Example:
Python
result = 7 / 3 # Result is 2.3333333333333335
In Python 3, the /
operator always performs float division, and if you want to perform floor division, you need to use the //
operator explicitly. In Python 2, the behavior of /
depends on the types of operands; if both operands are integers, it performs floor division, but if either operand is a float, it performs float division.
Here’s a summary:
- Floor Division (
//
): Returns an integer quotient, discarding the fractional part. - Float Division (
/
): Returns a float quotient, including the fractional part.
Choose the appropriate division type based on the desired result and the use case of your calculations.