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:

`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.`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.`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.`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.- 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 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:

`def is_leap_year(year):`

This line defines a function that will determine whether a given year is a leap year.`if year % 4 == 0:`

This line checks if the year is evenly divisible by 4. If it is, it could be a leap year.`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.`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.`year = int(input("Enter a year: "))`

This line prompts the user to enter a year and stores the input in the variable`year`

.`if is_leap_year(year):`

This line calls the function`is_leap_year()`

with the year that the user entered.`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:

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:

- Write a Python program to print the number of elements present in an array
- Python program to display the current date and time
- Python Program for Binary Search
- Python program to print prime numbers

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etcâ€¦ for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.