[Tutor] How to test for a remainder from division

Eric Walstad eric at ericwalstad.com
Mon May 14 23:45:14 CEST 2007


Hey Matt,

Skirting your question that looks like a modulo issue[1]...

Maybe isleapyear[2] will work for you:

import calendar

calendar.isleap(2007)  -> False
calendar.isleap(2008)  -> True


I hope that helps,

Eric.
[1] http://docs.python.org/ref/binary.html
[2] http://docs.python.org/lib/module-calendar.html

Matt Smith wrote:
> Hi there,
> 
> I'm trying to write a short function to test whether a year is a leap
> year or not. To do this I need to check whether the year divides exactly
> by 4, 100 and 400. I can't think of an easy way to test whether there is
> a remainder or not. The best I can come up with so far is:
> 
> if (year / 4.0) - (year // 4.0) <> 0:
> 
> This doesn't seem to work, it is always True, is there a problem with
> the comparison? The arithmetic seems to be the correct way to isolate
> the remainder of the division.
> 
> Can anyone suggest a better way of performing this test or alternately,
> how can I get the line above to work.
> 
> Thanks,
> 
> Matt
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
--
_________________________

Eric Walstad
2856 Diamond Street
San Francisco, CA 94131
eric at ericwalstad.com
415-864-4224
_________________________


More information about the Tutor mailing list