[Tutor] Leap years

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Mon Jan 13 06:40:09 2003


> > > def isLeapYear(year):
> > >     if year passes test for a leap year:
> > >         return 1
> > >     else:
> > >         return 0
> >
> > I think it's better to use True and False here.
> > Instead of "return 1", "return True".
> > Instead of "return 0", "return False".

Hi Don,

But even better than explicitely returning True or False is to avoid using
the 'if' statement altogether.  The pseudocode above can be written like
this:

###
def isLeapYear(year):
    return (year passes test for a leap year)
###

That is, if we're going to do something like:

   if something is true:
       return true
   else:
       return false

it's usually an improvement to be direct and just say

    return (something is true)

which is concise and avoids the controversy of using 0/1/True/False
altogether.  *grin*


Best of wishes to you!