w3resource

Python Exercises: Count the number of leap years within the range


Count leap years in a year range.

Write a Python program that counts the number of leap years within the range of years. Ranges of years should be accepted as strings.

Sample Data:
("1981-1991") -> 2
("2000-2020") -> 6

Sample Solution-1:

Python Code:

# Define a function to count the number of leap years within a given range of years
def test(r_years):
    # Extract the start and end year from the input string
    start_year, end_year = map(int, r_years.split('-'))
    # Use a generator expression to count the leap years within the specified range
    return sum(is_leap_year(year) for year in range(start_year, end_year+1))

# Define a function to check if a given year is a leap year
def is_leap_year(y):
    # Check the leap year conditions and return True if it's a leap year, otherwise False
    if y % 400 == 0:
        return True
    if y % 100 == 0:
        return False
    if y % 4 == 0:
        return True
    else:
        return False

# Initialize a string representing a range of years
text = "1981-1991"

# Print the range of years
print("Range of years:", text)

# Print a message indicating the counting of leap years within the specified range
print("Count the number of leap years within the said range:")

# Call the function to count and print the result
print(test(text))

# Repeat the process with a different range of years
text = "2000-2020"

# Print the range of years
print("\nRange of years:", text)

# Print a message indicating the counting of leap years within the specified range
print("Count the number of leap years within the said range:")

# Call the function to count and print the result
print(test(text)) 

Sample Output:

Range of years: 1981-1991
Count  the number of leap years within the said range:
2
Range of years: 2000-2020
Count  the number of leap years within the said range:
6

Flowchart:

Flowchart:  Count the number of leap years within the range.

Sample Solution-2:

Python Code:

# Define a function to count the number of leap years within a given range of years
def test(r_years):
    # Convert the input range of years string into a list of integers
    r = [int(x) for x in r_years.split('-')]
    # Use a list comprehension to create a list of leap years within the specified range
    return [is_leap_year(int(x)) for x in range(r[0], r[1] + 1)].count(True)

# Define a function to check if a given year is a leap year
def is_leap_year(y):
    # Check the leap year conditions and return True if it's a leap year, otherwise False
    if y % 400 == 0:
        return True
    if y % 100 == 0:
        return False
    if y % 4 == 0:
        return True
    else:
        return False

# Initialize a string representing a range of years
text = "1981-1991"

# Print the range of years
print("Range of years:", text)

# Print a message indicating the counting of leap years within the specified range
print("Count the number of leap years within the said range:")

# Call the function to count and print the result
print(test(text))

# Repeat the process with a different range of years
text = "2000-2020"

# Print the range of years
print("\nRange of years:", text)

# Print a message indicating the counting of leap years within the specified range
print("Count the number of leap years within the said range:")

# Call the function to count and print the result
print(test(text))

Sample Output:

Range of years: 1981-1991
Count  the number of leap years within the said range:
2
Range of years: 2000-2020
Count  the number of leap years within the said range:
6

Flowchart:

Flowchart: Count the number of leap years within the range.

For more Practice: Solve these Related Problems:

  • Write a Python program to parse a year range given as a string (e.g., "1981-1991") and count the number of leap years in that range.
  • Write a Python program to use the calendar module to determine how many leap years exist between two years extracted from a string.
  • Write a Python program to implement a function that returns the leap year count from a year range input in the format "start-end".
  • Write a Python program to split a string representing a range of years, convert to integers, and count leap years using conditional logic.

Go to:


Previous Python Exercise: Hash elements.
Next Python Exercise: Insert space before capital letters in word.

Python Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.