Intro to python: using - in / and %

  • Context: Python 
  • Thread starter Thread starter Brown Arrow
  • Start date Start date
  • Tags Tags
    Intro Python
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 2K views
Brown Arrow
Messages
101
Reaction score
0
im reading a book intro to python, I am confused by what is ment by the following
>>>-17/10
-2
i do not understand this explain pls

NOT A HW
 
Physics news on Phys.org
The expression -17/10 uses integer division, in which the value returned (the quotient) is an integer.

It's probably a bit easier to understand if both numbers involved are positive. For example, 17/10 evaluates to 1. If you do the long division, you'll see that the quotient is 1 and the remainder is 7, which you can find by evaluating the expression 17 % 10.

With -17/10, it would seem reasonable that the quotient would be -1, but this would result in a remainder of -7. Python seems to like the remainders to be positive, and for this problem gives a remainder of + 3. I.e., -17/10 == 3. This means that -17/10 == -2 plus a remainder of 3. To check this, note that -17 = -2 * 10 + 3.

Hope this helps.