Ensure a variable is divisible by 4

MRAB google at mrabarnett.plus.com
Tue Dec 5 18:46:26 EST 2006


Jonathan Smith wrote:
> > <geskerrett at hotmail.com> wrote in message
> > news:1165252238.866708.293510 at j72g2000cwa.googlegroups.com...
> >> I am sure this is a basic math issue, but is there a better way to
> >> ensure an int variable is divisible by 4 than by doing the following;
> >>
> >> x = 111
> >> x = (x /4) * 4
> >>
> >> Just seems a bit clunky to me.
>
>
> if ( x % 4 ) == 0:
> 	whatever # x is divisible by 4
>
> modulus is your friend :)
>
> -smithj

<pendantic>
It's "modulo"; "modulus" is a different operation.
</pedantic>




More information about the Python-list mailing list