Leap year program in Python using function

In this Python tutorial, I will show you, the leap year program in Python using function. We will see, a leap year program in Python without using a function, and also, I will show you an example of a leap year in Python using a function.

Write a program to check leap year in Python

Let us write a program to check leap year in Python. Here is a simple program.

year = int(input("Enter a year: "))

if year % 4 == 0:
    if year % 100 == 0:
        if year % 400 == 0:
            print(year, "is a leap year")
        else:
            print(year, "is not a leap year")
    else:
        print(year, "is a leap year")
else:
    print(year, "is not a leap year")

Here’s a brief explanation of the code:

  1. year = int(input("Enter a year: ")): This line gets input from the user and converts it to an integer. This is the year that will be checked to see if it is a leap year.
  2. if year % 4 == 0:: This line checks if the year is evenly divisible by 4. If the year is not divisible by 4, it’s not a leap year. If it is, the program checks further conditions.
  3. if year % 100 == 0:: This line checks if the year is evenly divisible by 100. This is because, while most years divisible by 4 are leap years, years that are divisible by 100 are not leap years unless they meet an additional condition (they are divisible by 400). This is to keep the calendar year from drifting too far ahead of the solar year.
  4. if year % 400 == 0:: If the year is divisible by 100, this line then checks if it is also divisible by 400. If it is, then it is a leap year.
  5. The print(year, "is a leap year") and print(year, "is not a leap year") lines print the result, informing the user whether the year they entered is a leap year or not.

Example:

Let’s say you input the year 2000. It is divisible by 4, it is divisible by 100 (it’s a century year), and it is also divisible by 400. So, it meets the conditions for being a leap year. The output would be 2000 is a leap year. Check the screenshot for the output.

leap year program in python

Leap year program in Python using function

Now. let us write a program to check leap year in Python using a function.

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

year = int(input("Enter a year: "))
if is_leap_year(year):
    print(year, "is a leap year")
else:
    print(year, "is not a leap year")

Let’s break this down:

  1. def is_leap_year(year): This line defines a function that will determine whether a given year is a leap year.
  2. if year % 4 == 0: This line checks if the year is evenly divisible by 4. If it is, it could be a leap year.
  3. if year % 100 == 0: If the year is evenly divisible by 4, this line then checks if it is also evenly divisible by 100. If it is, it might not be a leap year. This is because in the Gregorian calendar, century years are not leap years unless they are divisible by 400.
  4. if year % 400 == 0: If the year is evenly divisible by 100, this line checks if it is also evenly divisible by 400. If it is, then it is a leap year.
  5. year = int(input("Enter a year: ")) This line prompts the user to enter a year and stores the input in the variable year.
  6. if is_leap_year(year): This line calls the function is_leap_year() with the year that the user entered.
  7. print(year, "is a leap year") and print(year, "is not a leap year") These lines print whether the entered year is a leap year or not.

Example:

Let’s say you input the year 2024. It is divisible by 4, it is not divisible by 100 (it’s not a century year), so it meets the conditions for being a leap year. The output would be 2024 is a leap year.

You can check the screenshot below:

leap year in python using function

In this Python tutorial, I have shown you two examples of how to check leap year in Python using a function and without using any function. I hope you liked the Python program for leap year using a function.

You may also like: