[Tutor] How to test for a remainder from division

Eric Walstad eric at ericwalstad.com
Tue May 15 17:40:16 CEST 2007


Hey Matt,

Matt Smith wrote:
> I guessed that there would be a module out
> there providing a function to do this but wanted to go through the
> process as a learning exercise.
Good form, old boy.


> Here is the final code I have come up with, any comments?
I think your code looks fine.  I like to do the following, just to be a
little more concise:

def is_leap_year(year):
    return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

That is, the 'if' in your code is evaluating the statement into a
boolean.  Why not just return the results of that evaluation?


More information about the Tutor mailing list