Multiples of a number

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sun Nov 6 05:20:35 CET 2005


On Sun, 06 Nov 2005 02:39:40 +0100, Carl Friedrich Bolz wrote:

> 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:

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

Another way is, if you aren't doing anything *except* counting for the
other 99 values of x, just skip them completely:

fox x in range(20):
    do_something(x*100)


Ivan, what are you trying to do?


-- 
Steven.




More information about the Python-list mailing list