TechAid24

Your Technical Consultant

Check Leap Year in Python – Easy Python Program with Explanation

Posted on by Anonymous Guest 2 minutes read

A leap year is a year, occurring once every four years, which has 366 days, including 29 February as an intercalary day. 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. In this article, we will create a Python program to check a leap year. Create a Python program to check if the year is a leap year or not.

Method 1

year= int(input('Year: '))
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
    print(year,"is a Leap Year")
else:
    print(year," is not a leap year")
Output of method 1
Year: 2004
2004 is a Leap Year

In the above program, first, we are taking input from the user and then converting it to an integer using the int() method. Then we perform mathematical operations against the logic that a leap year is exactly divisible by 4, except for century years; the century year is a leap year only if it is perfectly divisible by 400.

Method 2

Python ships with a module called calendar that has a isleap() method that returns a boolean True if the year is a leap year.

from calendar import isleap 

year = int(input("Year :"))

if isleap(year):
    print(year,"is a Leap year")
else:
    print(year,"is not a Leap year")
Output of method 1
Year :2000
2000 is a Leap year

First, we are importing the isleap() method from the calendar module. Later, we pass the year as an argument to the method, along with conditional statements to print out the result.

Check Leap Year in Python

Tags:

Anonymous Guest

An anonymous author is a writer who conceals their true identity when publishing content, often for privacy, security, or confidentiality reasons. This approach is often used in technical articles to focus on the information being presented rather than the author's personal identity.

Leave a Reply

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