How to determine whether a year is a leap year?

I am trying to make a simple calculator to determine whether or not a certain year is a leap year.

By definition, a leap year is divisible by four, but not by one hundred, unless it is divisible by four hundred.

Here is my code:

def leapyr(n):
if n%4==0 and n%100!=0:
if n%400==0:
print n, " is a leap year."
elif n%4!=0:
print n, " is not a leap year."
print leapyr(1900)

When I try this inside the Python IDLE, the module returns None. I am pretty sure that I should get 1900 is a leap year.

import calendar
print calendar.isleap(1900)

Python provides this functionality already in the library module 'calendar'.

As a one-liner function:

def is_leap_year(year):
"""Determine whether a year is a leap year."""

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

It's similar to the Mark's answer, but short circuits at the first test (note the parenthesis).

As P. Ortiz noticed in comments, the standard library's calendar.isleap has the same implementation, so this function just saves you an import.

You test three different things on n:

n % 4
n % 100
n % 400

For 1900:

1900 % 4 == 0
1900 % 100 == 0
1900 % 400 == 300

So 1900 doesn't enter the if clause because 1900 % 100 != 0 is False

But 1900 also doesn't enter the else clause because 1900 % 4 != 0 is also False

This means that execution reaches the end of your function and doesn't see a return statement, so it returns None.

This rewriting of your function should work, and should return False or True as appropriate for the year number you pass into it. (Note that, as in the other answer, you have to return something rather than print it.)

def leapyr(n):
if n % 400 == 0:
return True
if n % 100 == 0:
return False
if n % 4 == 0:
return True
else:
return False
print leapyr(1900)

(Algorithm from Wikipedia)

The whole formula can be contained in a single expression:

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

print n, " is a leap year" if is_leap_year(n) else " is not a leap year"

Your function doesn't return anything, so that's why when you use it with the print statement you get None. So either just call your function like this:

leapyr(1900)

or modify your function to return a value (by using the return statement), which then would be printed by your print statement.

Note: This does not address any possible problems you have with your leap year computation, but ANSWERS YOUR SPECIFIC QUESTION as to why you are getting None as a result of your function call in conjunction with your print.

Explanation:

Some short examples regarding the above:

print 'the result is:', n1 + n2  # prints but uses no *return* statement

return n1 + n2    # returns the result to caller

Now when I call them:

this gives:

the result is: 15
None

The first line comes form the print statement inside of add2(). The None from the print statement when I call the function add2() which does not have a return statement, causing the None to be printed. Incidentally, if I had just called the add2() function simply with (note, no print statement):

I would have just gotten the output of the print statement the result is: 15 without the None (which looks like what you are trying to do).

Compare this with:

which gives:

15

In this case the result is computed in the function add2_New() and no print statement, and returned to the caller who then prints it in turn.

A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400. For example,

if( (year % 4) == 0):
if ( (year % 100 ) == 0):

if ( (year % 400) == 0):

print("{0} is a leap year".format(year))
else:

print("{0} is not a leap year".format(year))
else:

print("{0} is a leap year".format(year))

else:

print("{0} is not a leap year".format(year))

If you don't want to import calendar and apply .isleap method you can try this:

def isleapyear(year):
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
return True
return False