Python Floor Division

Summary: in this tutorial, you’ll learn about Python floor division operator (//) or mod.

Introduction to Python floor division

Suppose you have a division of two integers:

101 / 4

In this division, 100 is called a numerator (D) and 4 is called a denominator (N).

The integer division 101/ 4 returns 25 with the remainder 1. In other words:

101 / 4 = 25 with remainder 1
Code language: plaintext (plaintext)

Or put it in another way:

101 = 4 * 25 + 1
Code language: plaintext (plaintext)

Python uses two operators // and % that returns the result of the division:

101 // 4 = 25 101 % 4 = 1
Code language: plaintext (plaintext)

The // is called the floor division operator or div. And the % is called the modulo operator or mod.

This tutorial focuses on the floor division operator. You’ll learn about the modulo operator in the nex tutorial.

Both floor division and modulo operators satify the following equation:

101 = 4 * (101 // 4) + (101 % 4) 101 = 4 * 25 + 1
Code language: plaintext (plaintext)

Generally, if N is the numerator and D is the denominator, then the floor division and modulo operators always satisfy the following equation:

N = D * ( N // D) + (N % D)
Code language: plaintext (plaintext)

The floor division in Python

To understand the floor division, you first need to understand the floor of a real number.

The floor of a real number is the largest integer that is less than or equal to the number. In other words:

floor(r) = n, n is an integr and n <= r
Code language: plaintext (plaintext)

For example, the floor of 3.4 is 3 because 3 is the largest integer which is less than or equal to 3.4. The floor of 3.9 is also 3. And the floor of 3 is 3 obviously:

floor(3.4) = 4 floor(3.9) = 3 floor(3) = 3
Code language: plaintext (plaintext)

For the positive numbers, it would be easy to understand the definition. However, you should pay attention when it comes to negative numbers.

For example, the floor of -3.4 returns -4, not -3 based on the floor definition. Similarly, the floor of -3.9 also returns -4 .

floor(-3.4) = -4 floor(-3.9) = -4 floor(-3) = -3
Code language: plaintext (plaintext)

The floor division can be defined as:

n // d = floor(n/d)
Code language: plaintext (plaintext)

Notice that the floor division of a number is not always the same as truncation. The floor division is the same as truncation only when the numbers are positive.

Python floor division operator examples

The following example uses the floor division operators with positive and negative integers:

a = 10 b = 3 print(a//b) # 3 a = -10 b = -3 print(a//b) # 3 a = 10 b = -3 print(a//b) # -4 a = -10 b = 3 print(a//b) # -4
Code language: plaintext (plaintext)

Output:

3 3 -4 -4
Code language: plaintext (plaintext)

The following table illustrates the floor division of two integers a and b:

aba // b
1033
-10-33
10-3-4
-103-3

Python math.floor() function

The floor() function of the math module returns the floor division of two integers. For example:

from math import floor a = 10 b = 3 print(a//b) # 3 print(floor(a/b)) # 3
Code language: Python (python)

Output:

3 3

As you can see clearly from the output, the floor() function returns the same result as the floor division operator (//). It’s also true for the negative numbers:

from math import floor a = 10 b = -3 print(a//b) # -4 print(floor(a/b)) # -4
Code language: Python (python)

Output:

-4 -4

Summary

  • Python uses // as the floor division operator and % as the modulo operator.
  • If the numerator is N and the denominator D, then this equation N = D * ( N // D) + (N % D) is always satisfied.
  • Use floor division operator // or the floor() function of the math module to get the floor division of two integers.
Did you find this tutorial helpful ?