Leap year

The provided Python code defines a function named is_leap_year that determines whether a given year is a leap year or not. The function takes an integer representing the year as its argument. It employs the conditions specified for leap years: a year must be divisible by 4 but not by 100, or it should be divisible by 400. If these conditions are met, the function returns True, indicating that the year is a leap year; otherwise, it returns False. To use the code, one can substitute the variable year_to_check with the desired year and observe whether the output indicates that the year is or is not a leap year. This code provides a straightforward and efficient way to ascertain leap years in Python.


Source code

def is_leap_year(year):
    """
    Check if a year is a leap year.

    Parameters:
    - year (int): The year to be checked.

    Returns:
    - bool: True if the year is a leap year, False otherwise.
    """
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
        return True
    else:
        return False

# Example usage:
year_to_check = 2024  # Replace this with the year you want to check
if is_leap_year(year_to_check):
    print(f"{year_to_check} is a leap year.")
else:
    print(f"{year_to_check} is not a leap year.")

Description

  1. Function Definition:

The code defines a function named is_leap_year that takes one parameter, year

  1. Leap Year Conditions:

The function checks two conditions to determine if a year is a leap year:

  • If the year is divisible by 4 but not divisible by 100 (year % 4 == 0 and year % 100 != 0).
  • If the year is divisible by 400 (year % 400 == 0).
  1. Return Statement:
  • If either of the conditions is true, the function returns True, indicating that the year is a leap year.
  • Otherwise, it returns False.
  1. Example Usage:

The code provides an example usage where the variable year_to_check is set to 2023. You can replace this with any year you want to check.

  1. Print Result:
  • The code then prints whether the year_to_check is a leap year or not based on the result of the is_leap_year function.

In summary, the code defines a function that encapsulates the conditions for determining leap years and provides a simple example to showcase its usage. It’s a modular and reusable piece of code for checking leap years in Python.


Screenshot


Download

By Admin

Leave a Reply

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