Last Updated: Sep 16, 2023
Easy

Floor Division in Python

Shubham Das
1 upvote

Introduction

Python is an easy language that programmers use worldwide due to its syntax. Python also provides many built-in functions. Code in Python is way too short compared to C, C++, or Java. Python is used for developing websites, software applications, task automation, data visualization, and data analysis. Today, we are going to look into a very important as well as interesting concept - Floor Division in Python. The // symbol in Python is used to perform floor division, which is also known as integer division. So let's deep dive into the concepts.

What is Floor Division?

Floor division is a mathematical operation that is used to divide two numbers and get the result in which the quotient is rounded down to the nearest integer value. The floor division operator is denoted by "//" in most programming languages.

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

In most programming languages, the floor division operator is denoted by "//". It is particularly useful when dealing with numbers that need to be truncated or rounded down to obtain a result that is useful for a particular application.

Understanding Floor Division using Python

In Python, floor division is a common operation used in various applications, including mathematical calculations, data analysis, and programming. Like the other languages, it is denoted by the "//" operator in Python. Floor division is also called integer division. This operator divides two numbers and returns the result rounded down to the nearest whole number. This operator will give an integer result if both numbers are integers, but if at least one of them is float, it will give a float output.

For example:

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

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

``3``

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.

Performing 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.

Performing 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.

Performing 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.

Understanding the 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.

Understanding the 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.

Understanding the 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.

Understanding the 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 (//)

Below are the key differences between them:

1. Result Type: Division (/) yields a floating-point result, retaining decimal values (e.g., 5 / 2 = 2.5), while the Floor Division (//) produces an integer result, discarding the decimal portion (e.g., 5 // 2 = 2)

2. Negative Numbers: Division (/) maintains the sign, producing negative results for negative operands (e.g., -5 / 2 = -2.5), while the Floor Division (//) rounds towards negative infinity (e.g., -5 // 2 = -3)

3. Common Use Cases: Division (/) is suitable for tasks requiring precise decimal results, like mathematical calculations, and Floor Division (//) is ideal for scenarios where we need whole numbers, such as counting items evenly

What is the difference between division and floor division in Python?

The division operatorin Python returns the exact result of the division operation as a floating-point number, while the floor division operator // returns the quotient rounded down to the nearest integer.

What is meant by floor division?

Floor division is a mathematical operation in Python 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.

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 learnt 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!