1.
Performing Floor Division in Python
1.1.
Python
2.
Regular Division in Python
2.1.
Python
3.
Floor Division with Negative Numbers
3.1.
Python
4.
Floor Division with Floats
4.1.
Python
5.
Floor Division and Modulo in Python
5.1.
Python
6.
math.floor() function in Python
6.1.
Python
7.
divmod() Function in Python
7.1.
Python
8.
Floor Division Precedence
9.
9.1.
1. Splitting up a sequence into equal parts
9.2.
2. Calculating the average of a list
9.3.
3. Converting seconds to minutes and seconds
10.
Division (/) vs Floor Division (//)
11.
11.1.
What is the floor function in Python?
11.2.
What is the difference between float division and floor division?
11.3.
What is floor division and modulus precedence in Python?
12.
Conclusion
Last Updated: Jul 3, 2024
Easy

# Floor Division in Python

Shubham Das
1 upvote

Floor division in Python is a mathematical operation, also known as integer division. The floor division in Python is performed by using the // operator. It divides the first number by another and rounds down the result to the nearest whole number and makes it equal to the math. floor() function.

For example, if we divide 10 by 3 using floor division, we get:

10 // 3 = 3

Here, the quotient is 3.33, but since we are using floor division, the result is rounded down to the nearest integer, 3

Python Floor division is a mathematical operation that divides two numbers and rounds the result down to the nearest integer. The floor division operator // is used for this operation, and it returns an integer result without any decimal places. It is beneficial when dealing with numbers that need to be truncated or rounded down to obtain a useful result for a particular application.

For example:

``````13 // 2 = 6
13.5 // 2 = 6.0``````

Also see, Python Filter Function

## Performing Floor Division in Python

In Python, the floor division operator is denoted by the "//" symbol. It can be used to divide two numbers and get the result in which the quotient is rounded down to the nearest integer value.

For example:

• Python

### Python

``x = 20y = 3result = x // yprint(result)``

Output

``6``

In this example, we are using the floor division operator to divide 20 by 3. Since the quotient is 6.66, the floor division operator rounds it down to 6 and assigns the result to the variable 'result'. The print statement then displays the value of 'result' as 6.

## Regular Division in Python

In Python, the regular division operator is denoted by the "/" symbol. It can be used to divide two numbers and get the result as a floating-point number.

For example:

• Python

### Python

``x = 20y = 3result = x / yprint(result)``

Output

``6.666666666666667``

In this example, we are using the regular division operator to divide 20 by 3. The result is a floating-point number with several decimal places. The print statement then displays the value of 'result' as 6.666666666666667.

## Floor Division with Negative Numbers

Following is an example of using the floor division operator with negative numbers:

Example

• Python

### Python

``x = -10y = 3result = x // yprint(result)``

Output

``-4``

In this example, we are using the floor division operator to divide -10 by 3. Since the quotient is -3.33, the floor division operator rounds it down to -4 and assigns the result to the variable 'result'. The print statement then displays the value of 'result' as -4.

Note that the result of the floor division with negative numbers may not always be intuitive. The floor division operator always rounds down the result to the nearest integer value, which may not be the same as a result obtained by truncating the decimal part. In the case of negative numbers, the floor division operator always rounds towards negative infinity, which means that the result is always less than or equal to the exact result of the division.

## Floor Division with Floats

Following is an example of performing floor division with floats:

• Python

### Python

``import mathx = 20.5y = 3.2result = math.floor(x / y)print(result)``

Output

``6``

In this example, we are using the math.floor() function to perform floor division on two floating-point numbers: 20.5 and 3.2. First, we divide 20.5 by 3.2, which gives us a quotient of 6.40625. Then, we use the math.floor() function to round the quotient down to the nearest integer value, which in this case is 6. Finally, we assign the rounded quotient to the variable 'result' and print it to the console.

## Floor Division and Modulo in Python

Floor division and modulo are two related but distinct operations in Python. Floor division, denoted by the double forward slash symbol "//", calculates the quotient of a division and rounds it down to the nearest integer. Modulo, denoted by the percent sign "%", calculates the remainder of a division.

Let’s consider an example,

• Python

### Python

``x = 20y = 3result1 = x // y # floor divisionresult2 = x % y # moduloprint(result1)print(result2)``

Output:

``````6
2``````

In this example, we first perform floor division on 20 and 3, which gives us a quotient of 6. Then, we perform modulo on 20 and 3, which gives us a remainder of 2. We assign the results of the two operations to variables 'result1' and 'result2', and print them to the console.

The floor division operation gives us the number of times 3 can be divided into 20 without any remainder, which is 6. The modulo operation gives us the remaining amount after we have divided 20 by 3 as many times as possible, which is 2 in this case.

## math.floor() function in Python

The math.floor() function is a built-in function in the Python math module that returns the largest integer value less than or equal to the specified number. It is commonly used to round down a floating-point number to the nearest integer.

For example:

• Python

### Python

``import mathx = 6.7result = math.floor(x)print(result)``

Output

``6``

In this example, we are using the math.floor() function to round down the floating-point number 6.7 to the nearest integer. The result is assigned to the variable 'result', which is then printed to the console. The output shows that the largest integer less than or equal to 6.7 is 6.

## divmod() Function in Python

The divmod() function in Python takes two arguments and returns a tuple containing the quotient and remainder of their division. The first argument is divided by the second argument, and the result is a tuple containing two values: the quotient and the remainder.

Here's the syntax for using divmod() function:

``divmod(a, b)``

Where a is the dividend and b is the divisor.

The divmod() function is useful when you need to perform both floor division and modulo operations at the same time, because it returns both values in a single function call. You can also use the // operator for floor division and the % operator for modulo operation separately, but using divmod() can make your code more concise and easier to read.

Here's an example of using the divmod() function:

• Python

### Python

``result = divmod(20, 3)print(result)``

Output:

``(6, 2)``

In this example, we use the divmod() function to divide 20 by 3, and the result is a tuple containing the quotient and remainder of the division. The quotient is 6 and the remainder is 2.

## Floor Division Precedence

Floor division has a higher precedence than regular division in Python. This means that if a mathematical expression contains floor and regular divisions, the floor division will be performed first. The result will be rounded to the nearest integer before any regular division.

For example, consider the following expression:

``8 / 2 // 3``

The expression first performs the floor division of 8 and 2, which is 4. Then, it performs the regular division of 4 and 3, which is approximately 1.33333333333. However, since the result of the floor division is rounded down to the nearest integer (4), the final result of the expression is 1.

## Advance Use of Floor Division

There are some cases of the advance use of floor division:

### 1. Splitting up a sequence into equal parts

If you have a sequence of data, such as a list or string, and you want to split it up into equal parts, you can use floor division to determine the number of items that should be in each part.

For example, if you have a list of 20 numbers and want to split it into 4 equal parts, you can use floor division to calculate that each part should have 5 items.

### 2. Calculating the average of a list

If you have a list of numbers and want to calculate the average, you can use floor division to divide the sum of the numbers by the list length. This will give you the whole number part of the average.

### 3. Converting seconds to minutes and seconds

If you have a duration in seconds and you want to convert it to minutes and seconds, you can use floor division to calculate the number of whole minutes in the duration, and modulo to calculate the remaining seconds. For example, if you have a duration of 125 seconds, you can use floor division to determine that it is 2 minutes long, and modulo to determine that there are 5 remaining seconds.

## Division (/) vs Floor Division (//)

### What is the floor function in Python?

The floor function in Python, math.floor(), returns the largest integer less than or equal to a given number. It essentially rounds down the input to the nearest integer value towards negative infinity.

### What is the difference between float division and floor division?

We get the exact result when two numbers are divided in the case of float division, and the result is always in the float data type. We get the floor value of the result in the case of floor division, and the result can be in either float or integer data type depending on the operands' data type.

### What is floor division and modulus precedence in Python?

The precedence order of floor division and modulus operators are the same in Python; thus, if both of them appear in the same expression, then we will evaluate the expression in left-to-right order.

## Conclusion

In this article, we discussed Floor Division in Python. We learned about the two types of division in Python, their examples, and the operators used in it.

We hope this blog on Floor Division in Python was helpful. You can also refer to other similar articles.

Happy Learning Ninja!

Live masterclass