Check if Year is Leap or Not using Python

In general, a year is considered leap year if its divisible by 4 but if year is a century year(ending with two zeroes) then it need to be divisible by 400 to a Leap Year. For example – 2020 is a leap year as its divisible by 4, while 2003 is not a leap year because it cannot be divided by 4.
In this article, I’ll discuss Python Code for Checking Whether some year is leap year or not.

Algorithm for Checking if Year is leap or not

  • If year is divisible by 4, then its Leap Year
  • If year is not divisible by 4, then its not a Leap Year
  • If year is a Century Year then it need to be divisible by 400 and 100

Python Code for Checking Leap Year

# Python Code for Checking if year is leap or not

year = int(input("Enter Year => "))

# Dividing year by 4
if (year % 4 == 0):
	if (year % 100 == 0):
		if (year % 400 == 0):
			print(year, "is a Leap year")
		else:
			print(year, "is not a Leap year")
	else:
		print(year, "is not a Leap year")
else:
	print(year, "is not a Leap year")

Output of Above Code

Enter Year => 2018
2018 is not a Leap year

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

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

Recent Posts