# Floor division in Python

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.

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 floating-point 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.

The above code provides the following output:

-4

### Using the `//` operator to implement floor division on floating-point numbers in Python.

The floor division operator handles floating-point 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 floating-point numbers in Python.

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.

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.

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.

import_contacts

## Related Posts

• 03 May

### Make requirements.txt in python

Table of ContentsWhat is the requirements.txt file in Python?Ways to make requirements.txt file in PythonUsing the pip to make requirements.txt in PythonUsing the conda command to make requirements.txt file in PythonUsing the pipreqs package to make requirements.txt file in PythonConclusion What is the requirements.txt file in Python? Every package is also updated regularly and features […]

• 03 May

### Create an Array of 1 to 10 in Python

Table of ContentsIntroductionHow to create an array of 1 to 10 in Python?Using the range() function to create an array of 1 to 10 in Python.Using list comprehension along with the range() function to create an array of 1 to 10 in Python.Using a user-defined function to create an array of 1 to 10 in […]

• 03 May

### Remove Urls from Text in Python

Table of ContentsIntroductionWays to remove URLs from Text in PythonUsing the re.sub() function to remove URLs from Text in PythonUsing the re.findall() function to remove URLs from Text in PythonUsing the re.search() function to remove URLs from Text in PythonUsing the urllib.urlparse class to remove URLs from Text in PythonConclusion In this post, we will […]

• 03 May

### Create a List from 1 to 100 in Python

Table of ContentsWays to create a list from 1 to 100 in PythonUsing the range() function to create a list from 1 to 100 in PythonUsing the numpy.arange() function to create a list from 1 to 100 in PythonUsing the for loop with range() to create a list from 1 to 100 in PythonConclusion In […]

• 16 April

### Return vs Print in Python

Table of ContentsReturn vs Print in PythonConclusion Return vs Print in Python In Python, we work with a wide range of functions that make our code simple. We can also create functions using the def keyword. A function can be defined as per our requirement and can display something or return a value. This is […]

• 22 March

### How to Unpack Dictionary in Python

Table of ContentsDictionaries in PythonHow to Unpack Dictionary in PythonUsing the Unpack Operator (**) to Unpack Dictionary in PythonUsing the for Loop to Unpack Dictionary in PythonUsing the iteritems() Function to Unpack Dictionary in PythonUsing the items() Function to Unpack Dictionary in PythonUsing the keys and values Functions to Unpack Dictionary in PythonConclusion In this […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.