# Python Code Example: check leap year in if else statement

A normal year has 365 days. Every four years, however, there is a leap year with 366 days. This is because the earth does not need exactly 365 days to travel once around the sun, but about a quarter day more. So a year has one day more every 4 years.

So that the calculation is correct, there is no leap year every 100 years. Nevertheless, every four hundred years a leap year takes place. This concerns the years 2000, 2400 etc.

## Code Example

``````def isLeapYear(year):
if (year % 4 != 0):
return False
elif (year % 400 == 0):
return True
elif (year % 100 == 0):
return False
else:
return True

year = 2020

if(isLeapYear(year)):
print(str(year) + " is a leap year")
else:
print(str(year) + " is not a leap year")``````

### Output

``2020 is a leap year``

### Function Definition: `isLeapYear(year)`

This function takes a single argument `year` and returns `True` if the year is a leap year, and `False` otherwise. Here’s how it works:

``````def isLeapYear(year):
if (year % 4 != 0):
return False
elif (year % 400 == 0):
return True
elif (year % 100 == 0):
return False
else:
return True``````

#### Leap Year Rules

A year is considered a leap year if:

1. It is divisible by 4.
2. However, if it is divisible by 100, it is not a leap year.
3. But if it is divisible by 400, it is a leap year.

The function implements these rules as follows:

Check if the year is not divisible by 4:

``````if (year % 4 != 0):
return False``````

If the year is not divisible by 4, it is not a leap year.

Check if the year is divisible by 400:

``````elif (year % 400 == 0):
return True``````

If the year is divisible by 400, it is a leap year.

Check if the year is divisible by 100:

``````elif (year % 100 == 0):
return False``````

If the year is divisible by 100 (but not by 400), it is not a leap year.

Otherwise, it is a leap year:

``````else:
return True``````

### Main Code

After defining the `isLeapYear` function, the code checks whether a specific year (2020 in this case) is a leap year and prints the result.

``````year = 2020

if(isLeapYear(year)):
print(str(year) + " is a leap year")
else:
print(str(year) + " is not a leap year")``````

#### Explanation:

1. Define the year to be checked:python
``year = 2020``

Here, `year` is set to 2020.

Call the `isLeapYear` function and check its result:

``````if(isLeapYear(year)):
print(str(year) + " is a leap year")
else:
print(str(year) + " is not a leap year")``````
• The `isLeapYear(year)` function is called with `year` as its argument.
• If the function returns `True`, the code prints that the year is a leap year.
• If the function returns `False`, the code prints that the year is not a leap year.