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.
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 = 20 y = 3 result = x // y print(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 = 20 y = 3 result = x / y print(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 = -10 y = 3 result = x // y print(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 math x = 20.5 y = 3.2 result = 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 = 20 y = 3 result1 = x // y # floor division result2 = x % y # modulo print(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 math x = 6.7 result = 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 (//)
Operation
Division (/)
Floor Division (//)
Operand Types
Can be used with any type
Can be used with any type
Result Type
Floating-point or integer
Integer
Behavior
Returns quotient with
Returns quotient with
decimal precision
floor rounding
Example
7 / 2 = 3.5
7 // 2 = 3
-7 / 2 = -3.5
-7 // 2 = -4
Frequently Asked Questions
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.