[Tutor] How to test for a remainder from division

Kent Johnson kent37 at tds.net
Tue May 15 03:14:56 CEST 2007


Eric Walstad wrote:
> 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

And one of the nice things about Python, as well as having lots of 
useful stuff built in, much of the useful stuff is itself written in 
Python making it very easy to look under the hood and see what is going 
on. Here is the implementation of calendar.isleap():

def isleap(year):
     """Return 1 for leap years, 0 for non-leap years."""
     return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

Kent


More information about the Tutor mailing list