Leap Year Program in Python using Function

In this Python tutorial, I will show you how to create leap year program in Python using function.

The leap year program is a very common program that every Python beginner must know as a programming exercise. I have explained it in a very easy way, using concepts about leap years and how to calculate them.

Let’s start,

Leap Year Program in Python using Function

What is a leap year in Python? A leap year is a year that is completely divisible by 4. This year has 366 days and one extra day, but remember, if the year is like 100, then it should also be divisible by 400.

Also, if the year is divisible by 4 and not divisible by 100, then that is considered a leap year.

Let’s create a function that accepts the year and tells if the given year is a leap year or not.

def is_leap_year(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")

Now, call the function is_leap_year() with the value 2000, as shown below.

is_leap_year(2000)
Leap Year Program in Python using Function

From the output, you can see that when the year 2000 is passed to the is_leap_year() function, it returns that ‘2000 is a leap year’.

Let me explain the code part, first, you have passed the year 2000 to the function as is_leap_year(2000). This function contains multiple if statements to identify whether a given year is a leap year or not.

READ:  Iterate over a list in Python

Then, it is checked if the given year is divisible by 4 using the if year % 4 == 0: if the year is divisible by 4, then again, the next statement checks if it is also divisible by 100 using if year % 100 == 0: if it is divisible, then the next statement checks if it is again divisible by 400 using if year % 400 == 0: if it is divisible, then it is confirmed that the given year is a leap year.

Otherwise, if any of the conditions fails, then the given number is not a leap year. So, in the above code, you have created a function named is_leap_year(), which tells if any given year is a leap year or not.

This is how to create a program to find the leap year in Python using function.

Python Program for Leap Year using Function with User Input

In the above section, you have created the function named ‘is_leap_year()’ where you need to pass the year manually, but you can also take the input from the user; for that, you will need to modify the function a little bit.

def is_leap_year():
    year = int(input("Enter the 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")

Now, simply call the function with any parameter value as shown below.

is_leap_year()

When you run the above code, it asks for “Enter the year”, so enter the year about which you want to know whether it is a leap year.

Python Program for Leap Year using Function with User Input

From the above output, you can see when you call the function ‘is_leap_year()’, it asks for the year; as soon as you enter the year 1900, it checks and returns the result as “1900 is not a leap year”.

READ:  SyntaxError: Unexpected EOF while parsing

To get the year input from the user, the function is modified by adding this line ‘year = int(input(“Enter the year: “))’ at the beginning of the function to get the input from the user.

This is how to write a Python program for leap year using function with user Input.

Conclusion

In this Python tutorial, you learned how to create leap year program in Python using function using the function.

You also created a custom function that accepts input from the user and checks whether the given input is a leap year.

You may like to read: