Multiples of a number

Carl Friedrich Bolz cfbolz at gmx.de
Sat Nov 5 20:39:40 EST 2005


Hi!

Ivan Shevanski wrote:
> I've searched on google for a bit but I can't seem to find a way to get 
> multiples of a number. . .For instance what would I do if I wanted 
> something to happen every time x reached a multiple of 100 in this 
> sample code:
> 
> x = 0
> while x < 2000:
>      x += 1
> 

The idea is to use to modulo operator % which gives you the residue of a 
number when divided by another number:

x = 0
while x < 2000:
     x += 1
     if x % 100 == 0:
          do_something()


Note that it is probably more appropriate to use a for loop:

for x in range(2000):
     if x % 100 == 0:
         do_something()


Cheers,

Carl Friedrich Bolz




More information about the Python-list mailing list