We’ll start with an overview of arithmetic operators in Python and learn how the floor division operator // works. Then, we’ll learn how to use other equivalent methods including functions from the math and operator modules to perform floor division. Let’s get started…

Arithmetic Operators in Python

In Python, you can use arithmetic operators to perform simple arithmetic operations on numbers of the int and float data types. These operators act on operands (the numbers) and return the result of the operation. The following table summarizes the arithmetic operators in Python and how they work: Let’s take a few examples that use these arithmetic operators. You can try out these examples in a Python REPL or in Geekflare’s online Python editor. In this example, num1 is 18 and num2 is 5. The division operation num1/num2 returns the result including the fractional part. The number 5 goes into 18 three times leaving a remainder of three. Therefore, the floor division operation, num1//num2, gives the quotient 3, while the modulo operator gives the remainder – also 3 in this case. This should give you an idea of how the division, floor division, and modulo operators work. Next, we’ll learn about the floor division operator in detail.

Floor Division Using the // Operator

Consider a division operation with a dividend and a divisor. In num1/num2, num1 is the dividend and num2 is the divisor. To perform floor division of num1 and num2, use num1//num2. The floor division operator does not ensure that the answer is always an integer. If either the dividend (num1) or the divisor (num2) is a float then the result of num1//num2 is a float. Here are a few examples. If you need the result to be an integer, then you need to explicitly cast it into an integer using int() function:

What Happens Under the Hood?

When you use the floor division operator //, the special method (also called dunder method) floordiv() gets called. Therefore, you can also use the floordiv() method on any integer or floating point number, as shown below:

Floor Division Using operator.floordiv()

💡 To perform floor division in Python, you can also use the floordiv() function in the operator module. Python’s operator module contains the definitions of efficient functions that can perform all arithmetic operations. Therefore, to perform floor division, you can also use the floordiv() function from the operator module – instead of the // operator. Using the floordiv() function from the operator module is equivalent to using the floor division operator.

Floor Division Using math.floor()

How Does the Floor Function Work?

To understand this better, let us take a few examples and visualize these numbers on a number line. Example 1: Consider the number 2.3. The largest integer that is less than or equal to 2.3 is 2; so floor(2.3) will return 2. Example 2: You can apply the same definition when working with negative numbers as well. Consider the number -1.7. The largest integer that is less than or equal to -1.7 is -2; so floor(-1.7) will return -2. Let’s verify the above results using the floor() function from the math module. To perform floor division, you can call the floor() function with num1/num2 as the argument. As it truncates or rounds down the result to the nearest integer, it’s equivalent to the floor division operation. You can explicitly import the floor() function from the math module, as shown: Alternatively, you can as well import only the math module and then access the floor() function using math.floor(). Unlike the floordiv() function from the operator module and the floor division operator //, using math.floor(num1/num2) ensures that the result is an integer. This method makes the code readable and eliminates the type-casting step.

Examples of Floor Division in Python

Let’s conclude our discussion with a practical example: Binary search. ✅ This algorithm works by dividing the search interval into half at each step. This is done depending on whether the midpoint of the interval matches the target (search ends as match is found!) or is less than or greater than the target. As the size of the array is reduced by half at each step, the midpoint does not always evaluate to an integer. Consider the following implementation of the binary search algorithm. The function binary_search() takes in a number (item) and a list (itemlist) and searches for the occurrence of the item in itemlist.

If the item is found, the function returns the index at which item occurs. Else, it returns None.

This implementation is functionally correct except that we have not accounted for the midPt not evaluating to an integer as the search proceeds. If we call the function, we run into a TypeError stating that the list indices must be integers or slices, not float. We modify the function definition to use the floor division operator:  The function returns the index at which the item 7 is found, which is index one.

Conclusion

I hope this tutorial helped you understand how to perform floor division in Python. Here’s a summary of the different methods you’ve learned:

In Python, a operator b performs the operation defined by the operator with a and b as the operands and returns the result of the operation. You can use Python’s floor division operator //; a//b returns the quotient of the division operation a/b. Alternatively, you can use the equivalent floordiv() function defined in Python’s operator module with the syntax: operator.floordiv(a,b) to get the result of a//b. All the above methods return the quotient but the data type can be a float or an int depending on the values of a and b. So you’ll have to cast the return value to an integer. The floor() function from Python’s math module can also be used to perform floor division: math.floor(a,b) is equivalent to a//b and returns an integer. When you want the result to be an integer, consider using the floor function from the math module.

Next, learn how to work with defaultdict in Python. 👩🏽‍💻

How to Perform Floor Division in Python - 65How to Perform Floor Division in Python - 67How to Perform Floor Division in Python - 25How to Perform Floor Division in Python - 80How to Perform Floor Division in Python - 71How to Perform Floor Division in Python - 71How to Perform Floor Division in Python - 71How to Perform Floor Division in Python - 78How to Perform Floor Division in Python - 61How to Perform Floor Division in Python - 72How to Perform Floor Division in Python - 24How to Perform Floor Division in Python - 77