/ → Floating point division
// → Floor division
Let’s see some examples in both Python 2.7 and in Python 3.5.
Python 2.7.10 vs. Python 3.5
print (2/3) ----> 0 Python 2.7 print (2/3) ----> 0.6666666666666666 Python 3.5Python 2.7.10 vs. Python 3.5
print (4/2) ----> 2 Python 2.7 print (4/2) ----> 2.0 Python 3.5Now if you want to have (in Python 2.7) the same output as in Python 3.5, you can do the following:
Python 2.7.10
from __future__ import division print (2/3) ----> 0.6666666666666666 # Python 2.7 print (4/2) ----> 2.0 # Python 2.7Whereas there isn't any difference between floor division in both Python 2.7 and in Python 3.5.
138.93//3 ---> 46.0 # Python 2.7 138.93//3 ---> 46.0 # Python 3.5 4//3 ---> 1 # Python 2.7 4//3 ---> 1 # Python 3.5The
101 / 4 = 25 with remainder 1
Code language: plaintext (plaintext)8 is called the floor division operator or div. And the101 / 4 = 25 with remainder 1
Code language: plaintext (plaintext)9 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
101 / 4 = 25 with remainder 1
Code language: plaintext (plaintext)6 is the numerator and101 / 4 = 25 with remainder 1
Code language: plaintext (plaintext)7 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 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
101 = 4 * 25 + 1
Code language: plaintext (plaintext)4 returns101 = 4 * 25 + 1
Code language: plaintext (plaintext)5, not101 = 4 * 25 + 1
Code language: plaintext (plaintext)6 based on the floor definition. Similarly, the floor of101 = 4 * 25 + 1
Code language: plaintext (plaintext)7 also returns101 = 4 * 25 + 1
Code language: plaintext (plaintext)5 .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:
101 / 4 = 25 with remainder 1
Code language: plaintext (plaintext)0Output:
101 / 4 = 25 with remainder 1
Code language: plaintext (plaintext)1The following table illustrates the floor division of two integers
101 = 4 * 25 + 1
Code language: plaintext (plaintext)9 and101 // 4 = 25 101 % 4 = 1
Code language: plaintext (plaintext)0:aba // b1033-10-3310-3-4-103-3Python math.floor() function
The
101 // 4 = 25 101 % 4 = 1
Code language: plaintext (plaintext)1 function of the101 // 4 = 25 101 % 4 = 1
Code language: plaintext (plaintext)2 module returns the floor division of two integers. For example:101 / 4 = 25 with remainder 1
Code language: plaintext (plaintext)2Output:
101 / 4 = 25 with remainder 1
Code language: plaintext (plaintext)3As you can see clearly from the output, the
101 // 4 = 25 101 % 4 = 1
Code language: plaintext (plaintext)1 function returns the same result as the floor division operator (101 / 4 = 25 with remainder 1
Code language: plaintext (plaintext)8). It’s also true for the negative numbers: