I'm creating a program to determine whether or not a given year is a leap year. I just wanted to get anyone's feedback on it. I am aware of the calendar module in Python. I just wanted to try it from scratch.

year=int(raw_input("Input year:"))

if (year%4==0 and year%100==0 and year%400==0)or (year%4==0 and year%100 !=0 and year%400==0)or(year%4==0 and year%100 !=0 and year%400!=0):
  print str(year)+" is a leap year"
    print str(year)+" is NOT a leap year"
  • \$\begingroup\$ Try pylint on your code \$\endgroup\$ Nov 26, 2014 at 10:40

1 Answer 1



Consider the if condition, which I have reproduced below with different alignment for ease of viewing…

if (year%4==0 and year%100==0 and year%400==0)or \
   (year%4==0 and year%100!=0 and year%400==0)or \
   (year%4==0 and year%100!=0 and year%400!=0):

It's clear that the first two lines can be collapsed due to redundancy. As for the third line, any number that is not divisible by 100 is automatically not divisible by 400 as well.

if (year%4==0 and year%400==0)or \
   (year%4==0 and year%100!=0):

That can be further simplified to:

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


Please take care to follow Python formatting conventions, especially the consistent indentation by four spaces, especially since whitespace is significant in Python.

year = int(raw_input("Input year: "))

if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
    print str(year) + " is a leap year"
    print str(year) + " is NOT a leap year"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.