Leap Year Check In Python
Published on July 21 2021
In this tutorial, you will learn how to check if a given year is leap year or not in python using if condition and python built in calendar module in detail.
Source code
input_year = int(input("Enter a year:"))
# Method - 1 -> using If condition with logic
def is_leap(year):
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
return True
return False
if is_leap(input_year):
print("It is a leap year")
else:
print("It is not a leap year")
# Method - 2 -> using Python built In method from Calendar module
import calendar
if calendar.isleap(input_year):
print("It is a leap year")
else:
print("It is not a leap year")
Video