Floor Division in Python
Last Updated :
02 Oct, 2023
Division Operators allow you to divide two numbers and return a quotient, i.e., the first number or number at the left is divided by the second number or number at the right and returns the quotient. This article will explain how to execute floor division in Python.
What is Floor Division?
Floor division is a division operation that returns the largest integer that is less than or equal to the result of the division. In Python, it is denoted by the double forward slash ‘//’.
Example
If we divide 7 by 3 using floor division, we get:
2
Methods to perform Floor Division
- Using the Double Forward Slash Operator (//)
- Using the math.floor() Function
Using the Double Forward Slash Operator (//)
Syntax: result = dividend // divisor
Floor Division with Integers
In the code below, we have performed floor division between two integers In result1 we are performing floor division between two integers, 10 and 3. The result of dividing 10 by 3 is 3.333…., but floor division returns the largest integer less than or equal to the result. Therefore, the result is 3.
Python3
result1 = 10 / / 3
print ( "Floor division of two integers :" , result1)
|
Output
Floor division of two integers : 3
Floor Division with Floating Numbers
In the code below, we have performed floor division between a floating number and an integer. In result2, we are performing floor division between a float (7.5) and an integer (2). The result of dividing 7.5 by 2 is 3.75, but floor division truncates the fractional part, resulting in 3.0.
Python3
result2 = 7.5 / / 2
print ( "Floor division of a float and an integer :" ,result2)
|
Output
Floor division of a float and an integer : 3.0
Floor Division with Negative Integers
In the code below, we have performed floor division between two negative integers. In result3, we have floor division between a negative dividend (-17) and a negative divisor (-5). The result of dividing -17 by -5 is 3.4, but floor division returns the largest integer less than or equal to the result. Therefore, the result is 3.
Python3
result3 = - 17 / / - 5
print ( "Floor division of two negative integers :" ,result3)
|
Output
Floor division of two negative integers : 3
Floor Division with Negative Integer and Positive Integer
In the code below, we have performed floor division between a positive and negative integer. In result4, we are performing floor division between two negative numbers, -17 and 5. The result of dividing -17 by 5 is -3.4, but floor division returns the largest integer less than or equal to the result. Therefore, the result is -4.
Python3
result4 = - 17 / / 5
print ( "Floor division with a negative dividend and positive divisor :" , result4)
|
Output
Floor division with a negative dividend and positive divisor : -4
Using the math.floor() Function
math.floor() Division with Integers
In result, we are performing floor division between a float (7.5) and an integer (2). The result of dividing 7.5 by 2 is 3.75, but floor division truncates the fractional part, resulting in 3.0.
Python3
import math
result = math.floor( 10 / 3 )
print (result)
|
Output
3
math.floor() Division with Negative Integers
This code uses math.floor() to round down the result of -10 / 3, and it prints the rounded-down integer value, which is -4.
Python3
import math
result = math.floor( - 10 / 3 )
print (result)
|
Output
-4
Difference between Division and Floor Division
The division operator / performs standard division, which can result in a floating-point number (a decimal). If both the dividend and divisor are integers, Python will perform integer division if the result is an integer; otherwise, it will produce a floating-point result.
Python3
result5 = 10 / 3
print (result5)
|
Output
3.3333333333333335
Whereas the floor division operator // performs division and returns the largest integer that is less than or equal to the division result. It truncates (rounds down) the fractional part of the result, ensuring that the result is always an integer.
Python3
result6 = 10 / / 3
print (result6)
|
Share your thoughts in the comments
Please Login to comment...