Python – Print number of leap years from given list of years


In school or college, working with the leap years would be tricky. In case, when we want to find whether the given year is a leap year or not, the simple method is to divide it by four. When the leap year is divisible by 4 and 100, it is a leap year and otherwise, it is not. When the user is given a list of years ranging from 10 to 100, then it is a tedious process and consumes so much time. In that situation, Python comes into the track with simple codes.

To print the number of leap years from the list, we need to understand what a leap year is. It comes at a time break of four years with 366 days and the other years have only 365 days. The one extra day of the leap year is added to the February month.

Approaches

Approach 1 βˆ’ Using the lambda function

Approach 2 βˆ’ Using the calendar module

Approach 3 βˆ’ Using Conditional Statement

Approach 1: Python program to print the number of leap years from the list of years using lambda function

The method of getting the length of the list is found by importing the calendar module along with the lambda function.

Algorithm

  • Step 1 βˆ’ The calendar module is imported to use the function named isleap().

  • Step 2 βˆ’ The list is initialized with elements that hold the year.

  • Step 3 βˆ’ The filter function is always used when defining the lambda function.

  • Step 4 βˆ’ The leap year from the list is printed along with the length of the leap years.

Example

#importing the library
import calendar
#initializing the list with integer elements
list1 = [1998, 1987, 1996, 1972, 2003]
#iterating through the list using for loop
#using the isleap(), filter() function is used to find the leap year
leaplist  = list(filter(lambda num: calendar.isleap(num), list1))
#returns the final length 
print("Leap years from the list are: " , leaplist)
print("Total number of leap years: ", len(leaplist))

Output

Leap year from the list are: [1996, 1972]
Total number of leap year: 2

Approach 2: Python program to print the number of leap years from the list of years using Conditional statement

The list of elements is initialized and with the help of a conditional statement, the requirement is met to find which one is the leap year.

Algorithm

  • Step 1 βˆ’ A list of years is assigned to a variable named β€œlist1” and an empty list is defined.

  • Step 2 βˆ’ Using the iteration, when the year is in the list it should satisfy that the year when divided by 4 must be equal to the number divided by 100.

  • Step 3 βˆ’ Or the simple calculation, when the year is divided by 400, it should be divisible.

  • Step 4 βˆ’ The process continues until it checks for all the elements in the list.

  • Step 5 βˆ’ Then the total number of leap years is returned.

Example

#initializes the list with years as input
list1 = [1998, 1987, 1996, 1972, 2003]
#initializing the empty list
leaplist = []
#for loop is used to iterate through the list
#condition is used to print the result
for num in list1:
	if ((num%4==0 and num%100!=0) or (num%400==0)):
		print("Leap year from the list is: ",num)
		leaplist.append(num)
#finally printing the number of leap years in the given list
print("Total number of leap years: ", len(leaplist))

Output

Leap year from the list is: 1996
Leap year from the list is: 1972
Total number of leap year: 2

Approach 3: Python program to print the number of leap years from the list of years using Calendar module

The leap year is identified from the list of elements by importing the calendar module. As the library is used, the conditional statement is not used.

Algorithm

  • Step 1 βˆ’ The required calendar module is imported to deal with the leap years.

  • Step 2 βˆ’ The list is initialized with a certain number of years and to store the leap year from the list is stored in the leaplist variable.

  • Step 3 βˆ’ The for loop is used to iterate through the list elements, with the help of isleap() function, it directly finds the leap year.

  • Step 4 βˆ’ The total number of leap years is printed when the condition is met.

Example

#importing the library
import calendar
#initializing the list with integer elements
list1 = [1998, 1987, 1996, 1972, 2003]
#initializing the empty list
leaplist = []
#iterating through the list using for loop
#using the isleap() function to find the leap year
for num in list1:
	leap = calendar.isleap(num)
	if leap:
		print("Leap year from the list is: ",num)
		leaplist.append(num)
#returns the final length 
print("Total number of leap years: ", len(leaplist))

Output

Leap year from the list is: 1996
Leap year from the list is: 1972
Total number of leap year: 2

Conclusion

The list data structure deals with the elements of different data types like integer numbers, float numbers, or strings. The elements need to be defined within the square brackets separated by a comma. The approaches are given with modules like calendar, DateTime, and using conditional statements.

Updated on: 29-Aug-2023

114 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements