Python Program to Check Leap Year or Not
In this snippet, we will learn how to check year is leap year or not in Python.
Determine if a year is a leap year
- If a year is evenly divisible by 4 means having no remainder then go to the next step. If it is not divisible by 4. It is not a leap year. For example, 1997 is not a leap year.
- If a year is divisible by 4, but not by 100. For example, 2012 is a leap year. If a year is divisible by both 4 and 100, go to the next step.
- If a year is divisible by 100, but not by 400. For example, 1900 then it is not a leap year. If a year is divisible by both, then it is a leap year. So 2000 is a leap year.
Example 1
Using if else:
def checkLeapYear(year):
if (year % 400 == 0) or (year % 100 != 0) and (year % 4 == 0):
print("{0} year is a leap year".format(year))
else:
print("{0} year is not a leap year".format(year))
# input year from user
year = int(input("Enter a year: "))
checkLeapYear(year)
Example 2
Using calendar linbrary:
def checkLeapYear(year):
# return true if year is a multiple
# of 4 and not multiple of 100.
# or year is multiple of 400.
import calendar
return (calendar.isleap(year))
# input year from user
year = int(input("Enter a year: "))
if (checkLeapYear(year)):
print("{0} year is a leap year".format(year))
else:
print("{0} year is not a leap year".format(year))
Md Obydullah
Software Engineer | Ethical Hacker & Cybersecurity...
Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.