Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

Python Program To Check Leap year

1 min 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.

Problem Definition

Create a Python program to check if the year is a leap year or not.

Method 1

Program

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

Year: 2004
2004 is a Leap Year

In the above program first, we are taking input from the user then converting it to an integer using the int() method. Then we are performing 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 which returns 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

Year :2000
2000 is a Leap year

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


PROGRAMS

Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read