How to find leap year in Python?

If you search in internet on how to find whether the given year is leap year in Python, You will get a huge list of websites with article. These articles explains on finding leap with multiple lines of code using the leap-year rule. However, there is a straight forward method in Python to find if the given year is leap or not. The method isleap() in calendar module return True if the given year is leap and False if it is not leap. Let’s see an example.

Code sample

import calendar

for year_i in range(2009,2021):
     print("Is", year_i, "a leap? ",  calendar.isleap(year_i))

find leap year in Python

More Tips

Reference


Leave your thoughts...

This site uses Akismet to reduce spam. Learn how your comment data is processed.