What is floor division in python example?

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, 101 is called a numerator (N) and 4 is called a denominator (D).

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 following tutorial.

Both floor division and modulo operators satisfy 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)

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 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 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
10 3 3
-10 -3 3
10 -3 -4
-10 3 -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 ?

What's floor division in Python?

In Python, we can perform floor division (also sometimes known as integer division) using the // operator. This operator will divide the first argument by the second and round the result down to the nearest whole number, making it equivalent to the math. floor() function.

What is an example of a division operation in Python?

In Python 3. x, slash operator ("/") does true division for all types including integers, and therefore, e.g. 3/2==1.5. The result is of a floating-point type even if both inputs are integers: 4 / 2 yields 2.0.

What is meant by floor division?

Floor division is a normal division operation except that it returns the largest possible integer. This integer is either less than or equal to the normal division result. Floor function is mathematically denoted by this ⌊ ⌋ symbol.

What is floor division and modulus in Python?

Both are valid mathematical functions with different results. modulus. The modulus-function computes the remainder of a division, which is the "leftover" of an integral division. floor. The floor-function provides the lower-bound of an integral division.