## Introduction to Leap Year Program in Python

A year is considered a leap year if that year is exactly divisible by 4, except for years that end with 00(century year). A century year is a leap year if that is exactly divisible by 400. However, a year which not divisible by 400 and divisible by 100 is not a leap year.

**Example:**

- 2004 is a leap year.
- 2020 is a leap year.
- 1900 is not a leap year.
- 2013 is not a leap year.

### Examples of Leap Year in Python

Below are the different examples of leap year in python:

#### Example #1 – **Using Elif**

**Code:**

```
input_year = int(input("Enter the Year to be checked: "))
if (input_year%400 == 0):
print("%d is a Leap Year" %input_year)
elif (input_year%100 == 0):
print("%d is Not the Leap Year" %input_year)
elif (input_year%4 == 0):
print("%d is a Leap Year" %input_year)
else:
print("%d is Not the Leap Year" %input_year)
```

**Output:**

**Explanation:**

- First, the user will enter the year of his choice, which needs to be checked for leap year. As per the algorithm, if the year divisible by 400, that year is straightway leap year. And so the first if statement is “(input_year%400 == 0)”.
- Now when the year not divisible by 400, the second If statement will be executed that is “(input_year%100 == 0)”. If “(input_year%100 == 0)” evaluates to True. That means it’s a century year but not a leap year.
- Now when both the above statement doesn’t satisfy to True, then the third statement will be evaluated “input_year%4 == 0”. And if this satisfies, then that year is a leap year, Else not a leap year.

#### Example #2 – Using Nested If

**Code:**

```
input_year = int(input("Enter the Year to be checked: "))
if(input_year%4 == 0):
if(input_year%100 == 0):
if(input_year%400 == 0):
print("%d is Leap Year" %input_year)
else:
print("%d is Not the Leap Year" %input_year)
else:
print("%d is Leap Year" %input_year)
else:
print("%d is Not the Leap Year" %input_year)
```

**Output:**

**Explanation:**

- One needs to enter the year he/she wants to check for leap year. First condition “(input_year%4 == 0)” will checked. If it comes true, then it will be inside the nested if-else statement. Else If condition “(input_year%4 == 0)” evaluates to False, then straightway that year is not a leap year.
- Now “(input_year%4 == 0)” gets evaluated. Condition “(input_year%4 == 0)” is checked for century year. When condition “(input_year%100 == 0)” evaluates to False i.e. remainder comes means that year is not century year, and then that is a leap year.
- Condition “(input_year%100 == 0)” when evaluates to True i.e. remainder is zero.Then it goes the further nested if statement for checking the divisibility with 400. Now “(input_year%400 == 0)” gets evaluated. When the year is divisible by 400, that year is a leap year. Else, not a leap year.

#### Example #3 – Using Conditional Statement

**Code:**

```
input_year = int(input("Enter the Year to be checked: "))
if (( input_year%400 == 0)or (( input_year%4 == 0 ) and ( input_year%100 != 0))):
print("%d is Leap Year" %input_year)
else:
print("%d is Not the Leap Year" %input_year)
```

**Output:**

**Explanation:**

- Enter year, one wishes to check for leap/ non-leap year. Condition (input_year%400 == 0) OR That means any year which is exactly divisible by 400, is undoubtedly leap year. Condition (( input_year%4 == 0 ) AND ( input_year%100 != 0)). Here it comes from logic that a year divisible by 4, and not divisible by 100 is a leap year.
- First the condition “( input_year%4 == 0 )” will be evaluated. When it results in True, then condition “( input_year%100 != 0 ) “ will be evaluated. Condition “( input_year%100 != 0 ) “ is to evaluate whether year is century year or not. So, the inference can be made that a Non-century year is a leap year if it’s divisible by 4.

Here are the conditional table of “Or” and “And” to be referred to.

**OR:**

**AND**:

Then the result will be printed accordingly.

#### Example #4 – Using Function

**Code:**

```
#function defined
def LeapYearCheck(input_year):
if (input_year % 4) == 0:
if (input_year % 100) == 0:
if (input_year % 400) == 0:
return True
else:
return False
else:
return True
else:
return False
# Driver Code
input_year = int(input("Enter the Year to be checked: "))
if(LeapYearCheck(input_year)):
print("Leap Year")
else:
print("Not a Leap Year")
```

**Output:**

**Explanation:**

- This approach is similar to approach 2; it’s just the steps of approach 2(referred to above) is enclosed in the form of function. The function is called every time the year needs to be checked for leap/ non-leap year.
- Writing function for any task is a good practice in python. As it helps in the modularity of code and hence enhances the readability of code.

### Conclusion

Here we saw the logic of finding leap year and its various ways of implementation in Python. Here we have listed four main approaches; one can try on many other approaches as well. There are always many ways of doing the same thing. Here it’s just checking of year, so the speed is not much of matter. However, if logic is complex, speed does matter a lot.

### Recommended Articles

This is a guide to Leap Year Program in Python. Here we discuss the top examples of the Leap Year Program in Python and its code implementation. You can also go through our other related articles to learn more –