Write a Python Program to Check Leap Year or Not by using the If Statement, Nested If Statement, and Elif Statement with an example. Before we get into the Python leap year program, let us check the logic and see the definition behind this.

Leap Year logic: The normal one contains 365 days, but the leap year contains 366 days. Logically, All the years perfectly divisible by four are called Leap except the century. Century years mean they end with 00, such as 1200, 1300, 2400, 2500, etc. (Obviously, they are divisible by 100). For these, our Python program has to calculate further to check the Leap year logic.

- If the century is divisible by 400, that is a Leap year.
- If it is not divisible by 400, then that is not.

## Python Program to Check Leap Year using If Statement

This Python program allows the user to enter any number and check whether the user entered is a leap year or not using the If statement.

yr = int(input("Please Enter the Number you wish: ")) if (( yr%400 == 0) or (( yr%4 == 0 ) and ( yr%100 != 0))): print("%d is a Leap year" %yr) else: print("%d is Not" %yr)

In this Python leap year program, we used Logical AND and Logical OR operators Since we have to check multiple conditions within one If Statement. Let us divide the condition to understand it better

1: ( yr%400 == 0) OR

2: ( yr%4 == 0 ) AND

3: ( yr%100 == 0))

Within this Python Leap Year program code, the First condition (yr%400 == 0) will check whether the (yr%400) reminder is exactly equal to 0. As per the algorithm, any number divisible by 400 is a Leap year.

Or the Second If statement holds 2 statements with Logical AND operator, so they both have to be TRUE.

The first condition (yr%4 == 0) will check whether the remainder of the (yr % 4) is exactly equal to 0. If the condition is False, it will exit because there is no point in checking the other condition. It is not.

The Second condition will check (yr % 100) reminder is not equal to 0. If it is TRUE, the given number is not a century number.

As per the algorithm, any number divisible by 4 but not divisible by 100 is a Leap Year.

TIP: Please refer to Logical Operators to understand the functionality of Python Logical And and Logical Or.

### Using ternary operator

This example leap year code is the same as the above, but it uses the ternary operator to display the code in one line.

y = int(input("Enter a Year = ")) print(y, "is a leap year" if ((y % 4 == 0 and y % 100 != 0) or y % 400 == 0) else "is not")

```
Enter a Year = 1300
1300 is not
```

## Python Leap Year Program using calendar module

This program imports the calendar module and uses the isleap() function to determine whether the given year is a leap.

import calendar yr = int(input("Enter a Year = ")) if calendar.isleap(yr): print(yr, "is a Leap Year.") else: print(yr, "is not.")

## Python Program to Check Leap Year using Elif Statement

It allows the user to enter any value. Then, this program code will check whether the user entered is Leap year or not using the Elif Statement.

ya = int(input("Please Enter as you wish : ")) if (ya%400 == 0): print("%d is a Leap year" %ya) elif (ya%100 == 0): print("%d is Not" %ya) elif (ya%4 == 0): print("%d is a Leap year" %ya) else: print("%d is Not" %ya)

In this Python elif program, first, the User will enter any number to check whether that is Leap Year or Not.

- First, the If condition will check whether the (ya % 400) reminder is exactly equal to 0. As per the algorithm, any number divisible by 400 is a Leap year. If this condition Fails, then it will go to the next condition.
- The second If condition will check (ya % 100) reminder is exactly equal to 0 or not. As per the algorithm, any number not divisible by 400 but by 100 is Not a Century. We checked (ya % 400) in the First If statement. Because it failed, it came to a second condition. If both the first and second conditions fail, it will go to the third condition.
- The third condition in the Python leap year program will check whether num mod 4 is equal to 0. If this condition is True, then the given one is Leap because We already checked for the century years in the previous condition. If all the statements Fail, it will go to the Else statement at the end.
- If all the above statements fail, then it is not a Leap year

## Python Program to Check leap year using Nested If Statement

This leap year program uses nested if statement to check the lear year. It allows the user to enter any number, and then it will check using the Nested If statement.

num = int(input("Please Enter the Number you wish: ")) if(num%4 == 0): if(num%100 == 0): if(num%400 == 0): print("%d is a Leap Year" %num) else: print("%d is Not" %num) else: print("%d is a Leap Year" %num) else: print("%d is Not" %num)

This Python program allows users to enter any value to check whether that is a Leap year. First, the If condition will check whether the remainder of the (num%4) is exactly equal to 0.

- If the condition is False, the given number is not.
- If the condition is True, we must check further for the century. So, it will go to the Nested If condition.

Second If condition in the leap year program will check (num%100) whether the reminder is exactly equal to 0 or Not.

- If this condition is False, then it is not a century year. So the given number is Leap Year.
- If the expression is true, we must check whether the number is divisible by 400. So, the compiler will go to another Nested If condition.

This condition checks whether the remainder of the (num%400) is exactly equal to 0.

- If the expression is False, then the given number is not.
- If the expression is evaluated to True, then the given number is a Leap Year.

## Python Program to Check Leap Year Program in Python using functions

This function accepts any integer, and using the if else statement, it checks the condition and finds out whether it is a leap year.

def isleap(yr): if yr % 400 == 0 or (yr % 4 == 0 and yr % 100 != 0): return True else: return False yr = int(input("Enter a year = ")) if isleap(yr): print(yr, "is a leap year") else: print(yr, "is not")

### Using lambda function

This program uses the Python lambda function to check whether it is a leap year.

yr = int(input("Enter a year = ")) isleap = lambda yr: (yr % 4 == 0 and yr % 100 != 0) or yr % 400 == 0 if isleap(yr): print(yr, "is a leap year") else: print(yr, "is not")

```
Enter a year = 2200
2200 is not
```

### Using & operator

This approach performs bit manipulation.

yr = int(input("Enter a year = ")) isleap = (yr & 3 == 0) and ((yr % 25 != 0) or (yr & 15 == 0)) if isleap: print(yr, "is a leap year") else: print(yr, "is not")

```
Enter a year = 1920
1920 is a leap year
```

### Python Program to Check Leap Year using list comprehension

The list comprehension checks the complete condition in a single line. Next, use the if else to show the result.

yr = int(input("Enter a year = ")) isleap = any([yr % 400 == 0, yr % 4 == 0 and yr % 100 != 0]) if isleap: print(yr, "is a leap year") else: print(yr, "is not")

```
Enter a year = 2030
2030 is not
```

### Python Leap Year Program using datetime module

This program uses the datetime module and checks whether the second month has 29 days.

import datetime yr = int(input("Enter a year = ")) try: datetime.date(yr, 2, 29) print(yr, "is a leap year") except ValueError: print(yr, "is not")

```
Enter a yr = 1947
1947 is not
```