[ad_1]
In this post, we will see what is floor division in Python.
There are numerous types of operators in Python, all of which have different functioning and are used in different scenarios. One such operator provided in Python is the floor division operator, which comes under the subcategory of Arithmetic Operators. This tutorial discusses what is floor division in Python and the different ways available to implement it.
What is Floor Division in Python?
Floor Division is utilized to divide any two numbers and the quotient that we get from it is further rounded down to an integer value nearest to it. It holds the same precedence as that of division or multiplication.
x = 14 // 4 print(x) // Print 3

You might already know how basic division occurs in Python and before we focus more on floor division in Python, let us move on to understand what the math.floor()
function is and what it does.
What is the floor()
function in Python?
The floor()
function is a part of the math
module that provides generic functions to perform arithmetic operations in Python. The floor()
function takes any specified number as its argument and returns a value rounded down to an integer value nearest to it.
The combination of both division and flooring operations is the floor division operation. The double slash operator or the //
signs are used for floor division.
The floor division can be thought of as a regular division process along with the math.floor()
function call at the end.
Now, let us move on to the implementation of the floor division process.
How to implement floor division in Python?
Using the //
operator to implement floor division in Python.
The double slash operator is utilized for floor division in Python. Just like regular division, the syntax contains a divisor and a dividend, with the result of the process being the quotient.
The following code uses the //
operator to implement floor division in Python.
The above code provides the following output:
3
The double slash operator is predominantly utilized to implement floor division in Python. However, floor division is not just restricted to positive integers, it is capable of producing accurate results for negative numbers and floatingpoint numbers as well.
Using the //
operator to implement floor division on negative integers in Python.
Floor division can also deal with negative numbers. Although it might be a little confusing as floor division rounds down the number to a shorter one, and the number line becomes the complete opposite while moving towards the left of zero.
What this means is that if we take the equation  14 // 4
, then it returns 3.5
, which rounds off to 4
instead of 3
on flooring.
The following code uses the //
operator to implement floor division on negative numbers in Python.
x = – 14 // 4 print(x)

The above code provides the following output:
4
Using the //
operator to implement floor division on floatingpoint numbers in Python.
The floor division operator handles floatingpoint numbers as well, although the philosophy and the working does not change at all. Let us take an example code to make it clearer for the readers to understand.
The following code uses the //
operator to implement floor division on floatingpoint numbers in Python.
x = 14.0 // 4.0 print(x)

The above code provides the following output:
3.0
Using the //
operator to implement floor division with modulo in Python.
The modulo operator is utilized to return the remainder of the division process carried out between any two given numbers. The percentage sign (%)
is utilized to make use of the modulo operator. The modulo operator forms a relation together with the floor division operator, the equation of which is explained in detail below.
x = y * (x // y) + (x % y)

With x
being the dividend and y
is the divisor
.
Advanced use of floor division in Python.
We have covered the basic idea of the utilization and versatility of the //
operator. Now, let us find out the advanced use of floor division in Python.
Using the __floordiv__()
function to implement floor division in Python.
The __floordiv__()
function is naturally called when the //
operator is utilized. However, this function can also be called directly instead of utilizing the //
operator.
The following code uses the __floordiv__()
function to implement floor division in Python.
x = (14).__floordiv__(4) print(x)

The above code provides the following output:
3
The //
operator may provide errors when dealing with custom class objects, but the __floordiv__()
function extends the support further to custom class objects as well.
That’s all about what is floor division in Python.
[ad_2]