Simple % question

Chris Angelico rosuav at gmail.com
Tue Feb 11 20:36:37 EST 2014


On Wed, Feb 12, 2014 at 12:06 PM, Scott W Dunning <swdunning at cox.net> wrote:
> I just have a simple question. I don’t understand why 1%10 = 1?

The real question is: What do you expect that symbol to mean?
Its actual meaning is quite simple. In long division, dividing one
number by another looks like this:
 86528
 ____________
31415 ) 2718281828
 251320
 ------
 205081
 188490
 ------
 165918
 157075
 ------
 88432
 62830
 -----
 256028
 251320
 ------
 4708
(Monospaced font required here.)
2718281828 is the dividend; 31415 is the divisor. They're the numbers
we started with. At the top, we get the quotient, 86528, and down the
bottom, the remainder, 4708. Now let's ask Python about those numbers:
>>> 2718281828 // 31415
86528
>>> 2718281828 % 31415
4708
That's all it is. The // operator, when given two integers, will give
back an integer which is the quotient. The % operator (usually called
"modulo"), given the same two integers, gives you back the remainder.
With the dividend smaller than the divisor, your quotient is zero and
your remainder is the whole rest of the number; so 1 % 10 is 1. In
fact, 1 % anything greater than 1 will be 1.
Does that help?
ChrisA


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /