Floor Division Operator in Python
Floor Division in Python
In this post, you will learn about floor division in python with example and difference between division and floor division operator in python.
Let's get started.
What is Floor Division in Python?
Floor division is an arithmetic operator in python that performs normal division operation except that it returns the integer value rounded down to nearest integer.
It is denoted by double backward slash i.e //
The word 'floor' means down therefore in floor division we round down the values to nearest integer. Weather the value is 1.0, 1.1 or 1.9 we always round down to 1.
Example:
Program to demonstrate how to do floor division in Python:
Program:
a = 7
b = 2
c = a//b
print(c)
Output:
3
Explanation:
In the above program when the value of "a" is divided by "b" then the result is 3.5 but since it is floor division, therefore 3.5 is rounded down to nearest integer which is of course 3 and hence 3 is printed on the screen.
Difference between Division and Floor Division
Division | Floor Division |
---|---|
It returns both integer and floating point values when first operand is divided by second operand. | It returns only integer values when first operand is divided by second operand |
It does not round off the values. | It round down the value to its nearest integer. |
It is denoted by single backward slash i.e / | It is denoted by double backward slash i.e // |
Example program to show difference between division and floor division:
Program:
a = 7
b = 2
c = a/b
d = a//b
print(c)
print(d)
Output:
3.5
3