581

Is there a benefit to using one over the other? In Python 2, they both seem to return the same results:

>>> 6/3
2
>>> 6//3
2
Georgy
  • 12,464
  • 7
  • 65
  • 73
Ray
  • 187,153
  • 97
  • 222
  • 204
  • 3
    Please refer [The Problem with Integer Division](http://python-history.blogspot.com/2009/03/problem-with-integer-division.html) for the reason for introducing the `//` operator to do integer division. – Sriram Oct 30 '09 at 08:10

16 Answers16

774

In Python 3.x, 5 / 2 will return 2.5 and 5 // 2 will return 2. The former is floating point division, and the latter is floor division, sometimes also called integer division.

In Python 2.2 or later in the 2.x line, there is no difference for integers unless you perform a from __future__ import division, which causes Python 2.x to adopt the 3.x behavior.

Regardless of the future import, 5.0 // 2 will return 2.0 since that's the floor division result of the operation.

You can find a detailed description at PEP 238: Changing the Division Operator.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eli Courtwright
  • 186,300
  • 67
  • 213
  • 256
  • 2
    also `python -Qnew`. other division options: `-Qold` (default), `-Qwarn`, `-Qwarnall` – John La Rooy Nov 10 '09 at 00:13
  • 2
    Worth pointing out that `5.0 / 2` returns `2.5` in all versions, as does `5 / 2.0` - the old behaviour is only different when both operands are `int`. – Chris Aug 17 '16 at 15:45
  • 2
    What about when the numbers are negative? Is the behavior the same for negative integers? – Aaron Franke Mar 30 '18 at 04:17
  • 1
    @Srinivasu Your example isn't helpful. Better would be 5 // 2 (which yields 2) and -5 // 2 (which yields -3). – Marvin Jan 25 '19 at 04:36
  • In python 2.7.15, // behaviour is the same that python 3 – Alberto Perez Jan 29 '19 at 10:19
  • 1
    It is confusing to imply that ***floor division*** is synonymous with ***integer division.*** Afaik, -7 divided by 2 is -3 with integer division and -4 with floor division, so they are different. – Utkan Gezer Oct 09 '22 at 11:09
77

Python 2.x Clarification:

To clarify for the Python 2.x line, / is neither floor division nor true division.

/ is floor division when both args are int, but is true division when either of the args are float.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Yichun
  • 1,192
  • 9
  • 15
38

// implements "floor division", regardless of your type. So 1.0/2.0 will give 0.5, but both 1/2, 1//2 and 1.0//2.0 will give 0.

See PEP 238: Changing the Division Operator for details.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kena
  • 6,891
  • 5
  • 35
  • 46
  • This is a good answer. The PEP link is helpful. Also, consider using `math.floor()` or `math.fmod()` if you're not sure what's going on with the unary operators . – Scott Lowrey Apr 15 '15 at 16:29
  • `/` and `//` are bi-nary operators (two operands, left and right, numerator and denominator) – iono Jul 30 '19 at 12:00
35

/ → 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.5

Python 2.7.10 vs. Python 3.5

print (4/2)  ----> 2         Python 2.7
print (4/2)  ----> 2.0       Python 3.5

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

Whereas 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.5
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
N Randhawa
  • 8,773
  • 3
  • 43
  • 47
  • is this the same as int(5/2)? – PirateApp Mar 20 '18 at 10:52
  • What about when the numbers are negative? Is the behavior the same for negative integers? – Aaron Franke Mar 30 '18 at 04:18
  • 2
    **Re: Negatives** -- Behavior is the same, but remember that the result is floor, so rounding is always *down* towards *more negative*. Some examples: `-100 // 33` => **-4**; `100 // -33` => **-4**; but because of the rounding direction of floor func, the next one could seem counter-intuitive when compared to previous: `-100 // -33` => **3**. – Erdős-Bacon Aug 05 '19 at 19:36
  • 1
    @PirateApp - floor division ( x//y ) is the same as int(x/y) as long as the result is positive. If the result is negative, they differ - int() always rounds towards zero, while floor division always rounds down (i.e. to the left of the number line, or -∞). Also (as mentioned elsewhere), particularly large values may diverge - floor division will be exact as it's always handled as integers, while int(x/y) performs a floating-point calculation first, which is inexact – askvictor Apr 29 '22 at 00:42
23

As everyone has already answered, // is floor division.

Why this is important is that // is unambiguously floor division, in all Python versions from 2.2, including Python 3.x versions.

The behavior of / can change depending on:

  • Active __future__ import or not (module-local)
  • Python command line option, either -Q old or -Q new
u0b34a0f6ae
  • 48,117
  • 14
  • 92
  • 101
13
>>> print 5.0 / 2
2.5

>>> print 5.0 // 2
2.0
7

Python 2.7 and other upcoming versions of Python:

  • Division (/)

Divides left hand operand by right hand operand

Example: 4 / 2 = 2

  • Floor division (//)

The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity):

Examples: 9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4, -11.0//3 = -4.0

Both / division and // floor division operator are operating in similar fashion.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Abrar Ahmad
  • 109
  • 1
  • 3
6

// is floor division. It will always give you the integer floor of the result. The other is 'regular' division.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adam Bellaire
  • 108,003
  • 19
  • 148
  • 163
5

The double slash, //, is floor division:

>>> 7//3
2
ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Mark Roddy
  • 27,122
  • 19
  • 67
  • 71
3

The previous answers are good. I want to add another point. Up to some values both of them result in the same quotient. After that floor division operator (//) works fine but not division (/) operator:

>>> int(755349677599789174 / 2) # Wrong answer
377674838799894592
>>> 755349677599789174 // 2     # Correct answer
377674838799894587
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jaya ram
  • 31
  • 2
2

The answer of the equation is rounded to the next smaller integer or float with .0 as decimal point.

>>>print 5//2
2
>>> print 5.0//2
2.0
>>>print 5//2.0
2.0
>>>print 5.0//2.0
2.0
G.Ant
  • 29
  • 3
1

Python 3.x Clarification

Just to complement some previous answers.

It is important to remark that:

a // b

  • Is floor division. As in:

    math.floor(a/b)

  • Is not int division. As in:

    int(a/b)

  • Is not round to 0 float division. As in:

    round(a/b,0)

As a consequence, the way of behaving is different when it comes to positives an negatives numbers as in the following example:

1 // 2 is 0, as in:

math.floor(1/2)

-1 // 2 is -1, as in:

math.floor(-1/2)

villamejia
  • 394
  • 1
  • 6
  • 17
1

Python 3

Operation Result Notes
x / y quotient of x and y
x // y floored quotient of x and y (1)

Notes:

  1. Also referred to as integer division. The resultant value is a whole integer, though the result’s type is not necessarily int. The result is always rounded towards minus infinity: 1//2 is 0, (-1)//2 is -1, 1//(-2) is -1, and (-1)//(-2) is 0.

Python 2

Operation Result Notes
x / y quotient of x and y (1)
x // y (floored) quotient of x and y (4)(5)

Notes:

1. For (plain or long) integer division, the result is an integer. The result is always rounded towards minus infinity: 1/2 is 0, (-1)/2 is -1, 1/(-2) is -1, and (-1)/(-2) is 0. Note that the result is a long integer if either operand is a long integer, regardless of the numeric value.

4. Deprecated since version 2.3: The floor division operator, the modulo operator, and the divmod() function are no longer defined for complex numbers. Instead, convert to a floating point number using the abs() function if appropriate.

5. Also referred to as integer division. The resultant value is a whole integer, though the result’s type is not necessarily int.

iacob
  • 20,084
  • 6
  • 92
  • 119
0
  • // is floor division. It will always give you the floor value of the result.
  • And the other one, /, is the floating-point division.

In the following is the difference between / and //; I have run these arithmetic operations in Python 3.7.2.

>>> print (11 / 3)
3.6666666666666665

>>> print (11 // 3)
3

>>> print (11.3 / 3)
3.7666666666666667

>>> print (11.3 // 3)
3.0
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fatema Tuz Zuhora
  • 3,088
  • 1
  • 21
  • 33
0

Summary

  • x//y : EXACT integer division
  • int(x/y) OR math.floor(x/y): INEXACT integer division (but almost correct)
  • x/y: floating point division (that has the loss of significance)

Remarkable Calculation Result

import math
N = 1004291331219602346 # huge number 

print(N//100) #=> 10042913312196023 is correct answer
print(math.floor(N/100)) #=> 10042913312196024 is wrong answer
print(math.ceil(N/100)) #=> 10042913312196024 is wrong answer
print(int(N/100)) #=> 10042913312196024 is wrong answer

Consideration

I think about the evaluation of int(x/y).
At first, Python evaluate the expression x/y and get INEXACT floating number z.
Second, Python evaluate the expression int(z).
We get a wrong result when the loss of significance cannot be ignored.

naoki fujita
  • 689
  • 1
  • 9
  • 13
  • Re *"x//y EXACT integer division"*: That does ***not*** sound plausible. Can you provide some references? – Peter Mortensen Jan 30 '22 at 00:43
  • The [documentation](https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex) says: Integers have unlimited precision – oittaa Feb 06 '22 at 03:27
-2

5.0//2 results in 2.0, and not 2, because the return type of the return value from // operator follows Python coercion (type casting) rules.

Python promotes conversion of lower data type (integer) to higher data type (float) to avoid data loss.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131