How to check leap year in Python

Introduction:

In the Gregorian calendar, a leap year occurs every four years to account for the extra quarter day in the Earth's orbit around the sun. Leap years have 366 days instead of the usual 365, with February having an extra day. In this article, we will explore how to determine if a given year is a leap year using Python.


Step 1: Understanding the Leap Year Rule:

To identify a leap year, we need to consider the following rules:

1. If a year is divisible by 4, it is a leap year.

2. However, if a year is divisible by 100, it is not a leap year, unless...

3. If a year is divisible by 400, it is a leap year.


Step 2: Implementing the Leap Year Check in Python:

To check if a year is a leap year, we can create a Python function that follows the leap year rule.


```python

def is_leap_year(year):

    if year % 4 == 0:

        if year % 100 == 0:

            if year % 400 == 0:

                return True

            else:

                return False

        else:

            return True

    else:

        return False

```


Explanation:

- The `is_leap_year` function takes a year as an input parameter.

- We use nested `if` statements to apply the leap year rules.

- First, we check if the year is divisible by 4 using the modulo operator `%`. If it is, we proceed to the next check.

- Next, we check if the year is divisible by 100. If it is, we move to the innermost check.

- Finally, we check if the year is divisible by 400. If it is, the function returns `True`, indicating a leap year. If not, it returns `False`.


Step 3: Testing the Function:

To ensure the correctness of our function, we can test it with various inputs, including both leap and non-leap years.


```python

# Testing the function

years = [2000, 2004, 2008, 2012, 2020, 2100, 2200, 2300, 2400]


for year in years:

    if is_leap_year(year):

        print(f"{year} is a leap year.")

    else:

        print(f"{year} is not a leap year.")

```


Output:

```

2000 is a leap year.

2004 is a leap year.

2008 is a leap year.

2012 is a leap year.

2020 is a leap year.

2100 is not a leap year.

2200 is not a leap year.

2300 is not a leap year.

2400 is a leap year.

```


In this article, we've learned how to check for leap years in Python using the leap year rule. By implementing the `is_leap_year` function, you can easily determine if a given year is a leap year or not. Remember to keep these rules in mind when working with date-related calculations or calendar-based applications.


There is one more step to check leap year by Enter the Year:-

there is a code:-

def is_leap_year(year):
    if year % 4 == 0:
        if year % 100 == 0:
            if year % 400 == 0:
                return True
            else:
                return False
        else:
            return True
    else:
        return False

# Input the year from the user
year = int(input("Enter a year: "))

if is_leap_year(year):
    print(f"{year} is a leap year.")
else:
    print(f"{year} is not a leap year.")

Comments

Popular posts from this blog

Code of Animated login page and tips: