Ensure a variable is divisible by 4

david brochu jr brochu121 at gmail.com
Tue Dec 5 15:33:17 EST 2006


You can use the modulous "%" to check for a remainder of division. If no
remainder is found you know the number is divisible by 4.

Ex:

x = 111

if x%4 == 0:
   print "X is divisible by 4"

---------- Forwarded message ----------
From: "John Machin" <sjmachin at lexicon.net>
To: python-list at python.org
Date: 5 Dec 2006 11:26:49 -0800
Subject: Re: Ensure a variable is divisible by 4

Paul Rudin wrote:
> Max M <maxm at mxm.dk> writes:
>
> > geskerrett at hotmail.com skrev:
> >> Nick Craig-Wood wrote:
> >>> geskerrett at hotmail.com <geskerrett at hotmail.com> wrote:
> >>>>  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
> >
> > X *= 4
> >
> > ;-)
> >
>
>
> x=4
>
> :)

Ensure x is divisible by *any* non-zero integer:

x = 0

:-O
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20061205/5d16e73b/attachment.html>


More information about the Python-list mailing list