How to find out if february has 29 or 28 days ?

Andrew McNamara andrewm at object-craft.com.au
Tue Jul 30 20:47:34 EDT 2002


>A year is a leap year if it can be divided by 4, except if it can be 
>divided by 100, where it's a leap year only if it can be divided by 400. It 
>sounds awful to compute, but in Python, it's actually quite simple:
>
>>>> isLeap = lambda x: x % 4 == 0 and (x % 100 != 0 or x % 400 == 0)

Or, if you have a pathological aversion to lambda:

    >>> def isLeap(x):
    ...     return x % 4 == 0 and (x % 100 != 0 or x % 400 == 0)
    ... 
    >>> isLeap(1980)
    1
    >>> isLeap(1991)
    0
    >>> isLeap(2000)
    1
    >>> isLeap(1900)
    0

8-)

-- 
Andrew McNamara, Senior Developer, Object Craft
http://www.object-craft.com.au/




More information about the Python-list mailing list