1

Possible Duplicate:
What is the reason for having ‘//’ in Python?

While trying to do an exercise on summing digits, I stumbled on this solution:

def sum_digits(n):
   import math
   total = 0
   for i in range(int(math.log10(n)) + 1):
      total += n % 10
      n //= 10
   return total

My question is, what does the second to last line do? How is that proper syntax?

Community
  • 1
  • 1
Will
  • 207
  • 1
  • 5
  • 11

4 Answers4

3

That implements what is called floor division. Floor division (indicated by // here) truncates the decimal and returns the integer result, while 'normal' division returns the answer you may 'expect' (with decimals). In Python 3.x, a greater distinction was made between the two, meaning that the two operators return different results. Here is an example using Python 3:

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

Prior to Python 3.x, there is no difference between the two, unless you use the special built-in from __future__ import division, which then makes the division operators perform as they would in Python 3.x (this is using Python 2.6.5):

In [1]: 10 / 3
Out[1]: 3

In [2]: 10 // 3
Out[2]: 3

In [3]: from __future__ import division

In [4]: 10 / 3
Out[4]: 3.3333333333333335

In [5]: 10 // 3
Out[5]: 3

Therefore when you see something like n //= 10, it is using the same +=/-=/*=/etc syntax that you may have seen, where it takes the current value of n and performs the operation before the equal sign with the following variable as the second argument, returning the result into n. For example:

In [6]: n = 50

In [7]: n += 10

In [8]: n
Out[8]: 60

In [9]: n -= 20

In [10]: n
Out[10]: 40

In [11]: n //= 10

In [12]: n
Out[12]: 4
RocketDonkey
  • 36,383
  • 7
  • 80
  • 84
  • I'm not a python guy, but it seems weird to me it's a floor division and not a truncation division. If you want to simulate integer division, it seems like it would be difficult. – xaxxon Dec 03 '12 at 05:29
  • @xaxxon I may be misinterpreting, but I believe what you're saying is actually the same thing (floor division == truncation division) - the decimal part gets omitted (or more accurately, the `floor` function is run on it, returning the largest integer not greater than `x`. Does that answer the question? – RocketDonkey Dec 03 '12 at 05:37
  • What is the integer division result of -4 / 3? It would be -1, right? But the floor would be -2? – xaxxon Dec 03 '12 at 06:25
  • oh, jesus; ruby-1.9.2-p290 :001 > -4 / 3 => -2 – xaxxon Dec 03 '12 at 06:26
  • @xaxxon Haha. Yeah, with negatives it is essentially the same thing - largest integer value that is not greater than `x`. So `-4 / 3 = -1.33` would round to 2 (since -1 is greater than -1.33). Here is an article from Guido himself that goes into better detail: http://python-history.blogspot.com/2010/08/why-pythons-integer-division-floors.html – RocketDonkey Dec 03 '12 at 07:20
1

// is the floor division operator. It always truncates the return value to the largest integer smaller than or equal to the answer.

Jesse the Game
  • 2,600
  • 16
  • 21
1

The second to last line is a combination of operators, in a way, including an uncommon one, which is why it's a little confusing.

Let's piece it apart.

First, // in Python is floor division, which basically is division rounded down to the nearest whole number. Thus,

>>> 16//5
3
>>> 2//1
2
>>> 4//3
1
>>> 2//5
0

Finally, the = is there because of a Python syntax that allows one to perform an operation on a variable, and then immediately reassign the result to the variable. You've probably seen it most commonly in +=, as:

>>> a = 5
>>> a += 7
>>> a
12

In this case, //= means "perform floor division, floor dividing the variable by the second argument, then assign the result to the original input variable." Thus:

>>> a = 10
>>> a //= 6
>>> a
1
Community
  • 1
  • 1
jdotjdot
  • 16,134
  • 13
  • 66
  • 118
  • +1 man - sorry, missed you explaining the `+=`/`//=` syntax as I was typing - looks like we're on the same wavelength :) – RocketDonkey Dec 03 '12 at 05:01
  • Amazing how big a difference the 30 seconds earlier you posted your answer made – jdotjdot Dec 03 '12 at 07:05
  • Ha, yeah, it is interesting to watch. I've been on the 30-seconds after side many times - I think it all evens out in the long run :) – RocketDonkey Dec 03 '12 at 07:24
0

for the assignment in Python A += B equals to A = A + B ,A *= B equals to A = A * B same thing applies to "Floor Divide" as well , A //= B equals to A = A // B

Floor Division means return the truncated integer number

>>> 5 // 3   # 1.6
   1    # 0.6 will be throw off 
Shawn Zhang
  • 1,680
  • 1
  • 12
  • 21