Python Program to Check Leap Year

In this article we will learn about the leap year and how to check whether a year is a leap year or not. We will also learn how to write a code to check leap year program in python.

What is a Leap Year

A leap year is a year which has 1 extra day compared to ordinary year, meaning it has 366 days in total and 1 extra day lies in the month of february, which means february has 29 days instead of 28 days.

Mathematical Conditions for Leap Year

The mathematical conditions for the leap year are:

  • The year should be divisible by 400.
  • The year should be divisible by 4 but not divisible by 100.

    Examples of Leap Year

  1. 1947 is a leap year or not
    1947 is not a leap year as it is neither divisible by 4 nor by 400. It is defying both the conditions of leap year given above in mathematical conditions for the leap year section.

  2. 1900 is a leap year or not
    1900 is divisible by 4 and 100 but not by 400. As it is divisible by 4 but also by 100, so 1900 is not a leap year. The condition for leap year is the year should be divisible by 400 or the year should be divisible by 4 but not by 100.

  3. 2000 is a leap year or not
    2000 is a leap year as it is divisible by 400. It is satisfying the first condition of leap year given above in mathematical conditions for the leap year section.

  4. 2004 is a leap year or not
    2004 is a leap year as it is divisible by 4 but not by 100. It is satisfying the second condition of leap year given above in mathematical conditions for the leap year section.

Leap Year Program in Python

The following are different method to create leap year program in Python

1. Leap Year program in python using calendar module

The Calendar module allows you to output calendars like the Unix cal program and also provides additional functions related to the calendar. Monday is the first day of the week and Sunday is the last day of the week. In terms of weekly index Monday’s index is 0, Tuesday’s is 1, Wednesday’s is 2 Thursday’s is 3, Friday’s is 4, Saturday’s is 5 and Sunday’s is 6. We can also change the first day of the week by using setfirstweekday(integer) method, here the integer is the weekly index of the day you want to make the first day of the week.

In the leap year program of python we will use the isleap() method of the calendar module.

Syntax:

isleap(year)

isleap() method returns the boolean value depending on whether the year is leap year or not. If the year is leap year then isleap() method will return True else False.

Code for leap year program in python using calendar module

def checkLeapYear(year):
#importing the calendar module
    import calendar
#returning boolean value based on given year is leap year or not
    return(calendar.isleap(year))
    
# Driver Code for the leap year program in python 
#taking input (year) from the user
year = int(input("Enter the year: "))
if (checkLeapYear(year)):
#if checkLeapYear function returns true
    print("Leap Year")
else:
#if checkLeapYear function returns false
    print("Not a Leap Year")

OUTPUT:

Enter the year: 2000
Leap Year
Enter the year: 1900
Not a Leap Year

2. Leap Year program in python using if-else statement

  1. Take a year as input from the user.
  2. Check if the year is divisible by 400, if yes return true else go to step 3.
  3. Check if the year is divisible by 4, if yes go to step 4, else step 5.
  4. Check if the year is divisible by 100, if no return true, else go to step 5.
  5. Return false.

Source Code

def checkLeapYear(year):
	if year%400==0:
# if year is divisible by 400, return True
    		return True
	elif year%4==0:
# if year is divisible by 4,then check is it divisible by 100 or not
    		if year%100!=0:
#if year is not divisible by 100, then return True
        			return True
    		else:
#if year is divisible by 100, then return False
        			return False;
    
# Driver Code for the leap year program in python
#taking input (year) from the user
year = int(input("Enter the year: "))
if (checkLeapYear(year)):
#if checkLeapYear function returns true
	print("Leap Year")
else:
#if checkLeapYear function returns false
	print("Not a Leap Year")

OUTPUT:

Enter the year: 2000
Leap Year
Enter the year: 1900
Not a Leap Year

Summary

A leap year is a year with 366 days having one extra day in February month making it a month of 29 days.

Mathematical condition for a year to be a leap year is it should get divisible by 400 or it should get divisible by 4 but not by 100.

We can write a leap year program in python in two ways, one using calendar modules’ is leap method and one by simple if-else statement.

Other Python Programs
Python program to reverse a number
Python program for heap sort
Python program to check armstrong number
Python program to add two numbers
Python program to convert celsius to fahrenheit
Python program to find factorial of a number
Python program to find the middle of a linked list using only one traversal
Python program to reverse a linked list

Leave a Reply

Your email address will not be published. Required fields are marked *