491

I saw this in someone's code:

y = img_index // num_images

where img_index is a running index and num_images is 3.

When I mess around with // in IPython, it seems to act just like a division sign (i.e. one forward slash). I was just wondering if there is any reason for having double forward slashes?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pete
  • 5,791
  • 3
  • 19
  • 10

5 Answers5

633

In Python 3, they made the / operator do a floating-point division, and added the // operator to do integer division (i.e., quotient without remainder); whereas in Python 2, the / operator was simply integer division, unless one of the operands was already a floating point number.

In Python 2.X:

>>> 10/3
3
>>> # To get a floating point number from integer division:
>>> 10.0/3
3.3333333333333335
>>> float(10)/3
3.3333333333333335

In Python 3:

>>> 10/3
3.3333333333333335
>>> 10//3
3

For further reference, see PEP238.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
  • 26
    I actually like this style better... I can remember in atleast one language I've used (VB?) the differentiating factor was `/` vs `\ ` ... but I could never remember which was which! – Matthew Scharley Oct 08 '09 at 04:47
  • 3
    The PEP also addresses the fact that backslash is reserved for escape characters or escaping newlines, so that kind of immediately eliminated `\ ` as an operator. – Mark Rushakoff Oct 08 '09 at 04:54
  • 4
    Thank you, I have been busy with Python 3+ for quite some time, and I never new this, I have always done `int(n/i)`. Great info here! – Hidde Aug 19 '12 at 20:02
  • 1
    Python's 2 approach to "/" is not really bad. In SQL, we have to deal with it as well. Its common to see (0.0+variable1)/variable2 or 1.0*variable1/variable2 – Chris Oct 28 '15 at 13:42
  • This is almost like c++ int purpose when you jsut want to have non float value – George Go Aug 03 '21 at 08:04
  • Re *"...the // operator to do integer division"*: [Another answer](https://stackoverflow.com/questions/34217841/integer-division-in-python-3-strange-result-with-negative-number/34217875#34217875) claims *"// is* ***not*** *integer division"*. – Peter Mortensen Jan 29 '22 at 23:26
  • This answer is **wrong.** `//` does not perform integer division. `-5 // 2` results in `-3`, whereas it would be `-2` if it was indeed integer division. – Utkan Gezer Oct 09 '22 at 11:05
217

// is unconditionally "flooring division", e.g:

>>> 4.0//1.5
2.0

As you see, even though both operands are floats, // still floors -- so you always know securely what it's going to do.

Single / may or may not floor depending on Python release, future imports, and even flags on which Python's run, e.g.:

$ python2.6 -Qold -c 'print 2/3'
0
$ python2.6 -Qnew -c 'print 2/3'
0.666666666667

As you see, single / may floor, or it may return a float, based on completely non-local issues, up to and including the value of the -Q flag...;-).

So, if and when you know you want flooring, always use //, which guarantees it. If and when you know you don't want flooring, slap a float() around other operand and use /. Any other combination, and you're at the mercy of version, imports, and flags!-)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 14
    Interestingly, the truncating // still returns a float – Lucretiel Sep 18 '13 at 16:05
  • The // operator does floored division, not truncating/truncated division. – Peter John Acklam Mar 31 '15 at 08:04
  • This is the more direct answer based on the nature of this question, imho. The higher voted answer is useful but this one makes it crystal clear why you would choose one over the other in Python2. – Peter M. Elias Jan 13 '16 at 23:26
  • @PeterJohnAcklam that's true, but it's strange that // would return a float when, to many people, "floored division" and "integer division" are highly related. What's the most Pythonic way to get an integer from a floor division? int(a//b)? – Shuklaswag Dec 20 '17 at 16:50
  • @Lucretiel maybe that has changed in later versions of Python? `type(2//3)` in Python 3.8.6 returns `` for me. – Mark Ransom Jun 18 '21 at 14:27
27

To complement these other answers, the // operator also offers significant (3x) performance benefits over /, presuming you want integer division.

$ python -m timeit '20.5 // 2'
100,000,000 loops, best of 3: 14.9 nsec per loop

$ python -m timeit '20.5 / 2'
 10,000,000 loops, best of 3: 48.4 nsec per loop

$ python -m timeit '20 / 2'
 10,000,000 loops, best of 3: 43.0 nsec per loop

$ python -m timeit '20 // 2'
100,000,000 loops, best of 3: 14.4 nsec per loop
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3733083
  • 419
  • 4
  • 6
  • 2
    I tried this with [Python 3](https://en.wikipedia.org/wiki/History_of_Python#Version_3) (Python 3.8.5) and all four ran at ***the same speed***. Sample run: *9.24* nsec, *9.40*, *9.22*, and *9.34* nsec per loop. (CPU: AMD [FX-4300](https://en.wikipedia.org/wiki/List_of_AMD_FX_microprocessors#Piledriver_Core_(Vishera,_32_nm)) 3.8 GHz (four cores). Motherboard: Gigabyte Technology GA-970A-DS3P. Operating system: Linux ([Ubuntu MATE 20.04](https://en.wikipedia.org/wiki/Ubuntu_MATE#Releases) (Focal Fossa) with [Cinnamon](https://en.wikipedia.org/wiki/Cinnamon_(desktop_environment))).) – Peter Mortensen Dec 01 '20 at 10:34
  • 1
    Thus, the 3x performance benefits for the `//` operator is disputed. – Peter Mortensen Dec 01 '20 at 10:38
  • Still holds true for Python 2 which is still broadly used to this day – MacK Jan 28 '21 at 18:26
  • @MacK: [Turbo C++](https://en.wikipedia.org/wiki/Turbo_C%2B%2B) is also broadly used to this day in some places. Famously, some university interns in India are [forced to use Turbo C++ from the 1990s](https://www.quora.com/What-are-some-bitter-truths-about-engineering-in-India/answer/Adhokshaj-Mishra) (otherwise they can't complete their degrees). This has also been documented by Joshua Fluke - I don't have the exact reference, but it may be close to [this one](https://www.youtube.com/watch?v=ZTxcFhczzSo). – Peter Mortensen Jan 29 '22 at 23:49
23

To complement Alex's response, I would add that starting from Python 2.2.0a2, from __future__ import division is a convenient alternative to using lots of float(…)/…. All divisions perform float divisions, except those with //. This works with all versions from 2.2.0a2 on.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
11

// can be considered an alias to math.floor() for divisions with return value of type float. It operates as no-op for divisions with return value of type int.

import math
# let's examine `float` returns
# -------------------------------------
# divide
>>> 1.0 / 2
0.5
# divide and round down
>>> math.floor(1.0/2)
0.0
# divide and round down
>>> 1.0 // 2
0.0

# now let's examine `integer` returns
# -------------------------------------
>>> 1/2
0
>>> 1//2
0
Val Neekman
  • 17,692
  • 14
  • 63
  • 66