floor division operator

To calculate the floor division, we should use // operator.




Floor in Mathematics

floor(x) = The largest integer which is less than or equal to x.

floor(4) = 4

floor(4.5) = 4

floor(4.9) = 4




Need of floor division

In Python, the normal division always returns a float value. i.e with fractional part.

Example

10 / 2 will return 5.0.

20 / 5 will return 4.0

Example

#normal division always returns a float value

print(10/2)
print(20/5)





Floor division

If we expect integer result from the division operation, we should use // operator (floor division operator).


Example

#Floor division in python

#Normal division
print(5 / 2)    #it will return 2.5

#Floor division
print(5 // 2)   #it will return 2


Basically, it removes the fractional part from the result of a normal division.


Topics You Might Like