Calculate Sum Get User Input Patterns Program Find Area of CircleFind Area of Rectangle Perimeter of Rectangle Find Even and Odd Numbers Find Sum of Digit in Python Find Factorial in PythonPrint Fibonacci Series Check Armstrong Number Reverse a Number Check Palindrome Number Swap Two Numbers Matrix MultiplicationFind Perfect NumberAdd Two Matrix in Python Find Sum of Squares Find Sum of Cubes Find URL in String
Find Largest in List Find Duplicate in List Check Alphabet Remove Duplicate Elements from List Reverse List Elements Find Element in Python List Interchange first and last element in List Find Even Numbers in List Find Length of a List Find Sum of list Elements Find Elements in List Get the product of List elements Interchange List Elements Find Smallest Elements in List Find Max Value in a List
In this article, we write a python program to find whether a year is a leap year or not. In a leap year, there are 366 days instead of 365 days. A year is a leap year if the following conditions are met:
The year is multiple of 400.
The year is multiple of 4 and not multiple of 100.
Input year
if year % 400 == 0 then leap year
else if year % 100 == 0 then not a leap year
else if year % 4 == 0 then leap year
else not a leap year
All the programs are executed on the Python3.8 version.
This is a simple code for finding a leap year in Python. Here, we checked two major conditions, the first is divisible by 400 and the second is divisible by 4, rest in all the situations, the year will not be a leap year. See the code below.
je_year = int(input("Enter year"))
​
if(je_year % 400 == 0):
  print("leap year")
elif(je_year % 100 == 0):
  print("not a leap year")
elif(je_year% 4 ==0 ):
  print("leap year")
else:
  print("not a leap year")
Output:
Enter year 2021
not a leap year
Output 2:
Enter year 2020
leap year
If you are looking for a single-line solution then you can use this code. Here, we just combined all the conditions in a single statement by using conditional operators. See the example below.
je_year = int(input("Enter year"))
​
if(((je_year % 4 == 0) and (je_year % 100 != 0)) or (je_year % 400 == 0)):
  print("leap year")
else:
  print("not leap year")
Output:
Enter year 1893
not leap year
Python provides a built-in method. We can use python's inbuilt calendar.isleap() method to find whether a year is a leap year or not. This method just takes one argument: the year that needs to be tested. Look at the code below.
import calendar
​
je_year = int(input("Enter year"))
​
if(calendar.isleap(je_year)):
  print("Leap year")
else:
  print("not a leap year")
​Output:
Enter year 2016
Leap year