Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

Python Program to Check Leap Year

Posted in Python Tutorial
Updated on Oct 31, 2024
By Mari Selvan
πŸ‘οΈ 306 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
Python Program to Check Leap Year

Photo Credit to CodeToFun

πŸ™‹ Introduction

In the realm of programming, determining whether a given year is a leap year is a common requirement. A leap year is a year that is evenly divisible by 4, except for end-of-century years, which must be divisible by 400 to be a leap year. Checking for leap years involves implementing specific rules to validate the year.

In this tutorial, we will explore a Python program designed to check whether a given year is a leap year.

πŸ“„ Example

Let's delve into the Python code that accomplishes this task.

is_leap_year.py
Copied
Copy To Clipboard
# Function to check if a year is a leap year
def is_leap_year(year):
    # Leap year conditions
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
        return True  # It's a leap year
    else:
        return False  # It's not a leap year

# Driver program
# Replace this value with the year you want to check
year = 2024

# Call the function to check if the year is a leap year
if is_leap_year(year):
    print(f"{year} is a leap year.")
else:
    print(f"{year} is not a leap year.")

πŸ’» Testing the Program

To test the program with different years, modify the value of year in the code.

Output
2024 is a leap year.

Run the script to check if the specified year is a leap year.

🧠 How the Program Works

  1. The program defines a function is_leap_year that takes an integer year as input and returns True if the year is a leap year, and False otherwise.
  2. The function checks leap year conditions: the year must be divisible by 4 and not divisible by 100, or it must be divisible by 400.
  3. Replace the value of year in the main program with the desired year you want to check.
  4. The program calls the is_leap_year function and prints the result.

πŸ“ Between the Given Range

Let's dive into the python code that checks for leap years in the specified range.

check_leap_years.py
Copied
Copy To Clipboard
def check_leap_years(start_year, end_year):
    leap_years = []

    for year in range(start_year, end_year + 1):
        if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
            leap_years.append(year)

    return leap_years

# Set the range of years
start_year = 2024
end_year = 2050

# Call the function to check leap years
leap_years = check_leap_years(start_year, end_year)

# Output the leap years
print(f"Leap Years in the range {start_year} to {end_year}:\n{leap_years}")

πŸ’» Testing the Program

The provided code is set to check for leap years in the range from 2024 to 2050. You can modify the start_year and end_year variables to test the program with different ranges.

Output
Leap years in the range 2024 to 2050:
2024 2028 2032 2036 2040 2044 2048

Run the script to see the leap years in the specified range.

🧠 How the Program Works

  1. The program defines a function check_leap_years that takes a start year and an end year as input and returns a list of leap years within that range.
  2. Inside the function, it iterates through each year in the specified range and checks the leap year conditions.
  3. Leap years are added to the leap_years list.
  4. The main section sets the range from 2024 to 2050 and calls the function to check for leap years.
  5. The result is then printed in the specified output format.

🧐 Understanding the Concept of Leap Year

Before delving into the code, let's understand the concept of leap years. Leap years are years that are evenly divisible by 4, except for end-of-century years, which must be divisible by 400 to be a leap year.

🎒 Optimizing the Program

While the provided program is effective, consider exploring and implementing alternative approaches or optimizations for checking leap years.

Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
10 months ago

If you have any doubts regarding this article (Python Program to Check Leap Year), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy