Software, Tech & Coding simplified.

# How to Find a Leap Year in Python Use this Python function to check if a given year is a leap year.

```def is_leap(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)```

Example usage:

```print(is_leap(2000))  # --> True
print(is_leap(1900))  # --> False
print(is_leap(2016))  # --> True```

If you don’t need to implement the leap year functionality yourself, use the built-in `calendar` module’s `isleap` function:

```from calendar import isleap

print(isleap(2000))
print(isleap(1900))
print(isleap(2016))```

## More Details about Leap Years

You might know that a year is not exactly 365 days long. It’s actually around 365.24 days in true length. So to compensate for this, we need to add an extra day every four years to the calendar. A year with 366 days is called a leap year.

Definition: A leap year is a year that is (evenly) divisible by 4. The exception is the years that are divisible by 100 but not by 400.

Leap year examples:

• 2012
• 2016
• 1986

Examples of non-leap years:

• 2011
• 2003
• 1997
• 2009

Exceptions:

• 1900 is not a leap year because it is divisible by 4 and 100, but not by 400.
• 2000 is a leap year because it is divisble by 4, 100, and 400.

## Modulo—How to Calculate Remainders

Modulo returns the remainder of an integer division. In other words, it gives you the leftovers of a “fair division”.

For example, if you have 7 slices of pizza and 4 hungry guests, you can share one slice per person, but 3 slices are leftover. You could figure out the number of leftover pieces by calculating `7 modulo 4`, which gives `3`.

### Modulo in Python

In Python, the modulo operator is the percent sign `%`.

For example, you can calculate `7 modulo 4` in Python by:

`7 % 4`

## How to Find Leap Year in Python Using Modulo

To know if a year is a leap year, you can use modulo.

By definition, a leap year is evenly divisible by 4. But the exception is the years that are not divisible by 100 but are divisible by 400.

Here is one way you can turn this definition into Python code:

```def is_leap(year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
else:
return True
else:
return False```
• Here the outer if-else checks if the year is divisible by 4 or not. If it’s not, there is no way the year can be a leap year.
• The next if-else checks if the year is divisible by 100. If it is, the only way it can be a leap year is if it’s also divisible by 400. If it’s not, the year is not a leap year. And if the year is not divisible by 100, it must be a leap year (because it’s divisible by 4).

This approach works, but you can simplify it by flattening down the nested if-else expressions:

• If a year is divisible by 400, so it is a leap year.
• If a year is not divisible by 400 but is divisible by 100, so it is not a leap year.
• If a year is not divisible by 400 nor 100 but is divisible by 4, so it is a leap year.
```def is_leap(year):
if year % 400 == 0:
return True
if year % 100 == 0:
return False
if year % 4 == 0:
return True
return False```

And you can simplify it even further to get the simplest form possible:

```def is_leap(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)```

Now you know how to check if the current year is a leap year in Python. Next, let’s take a look at how you can figure out the next leap year.

## Next Leap Year in Python

To calculate the next leap year in Python:

1. Specify the current year.
2. Get the previous leap year.
3. Add 4 to the previous leap year it.
4. Check if the resulting year is a leap year.
5. If it’s not, add 4 years to the year until it is a leap year.

Here is how it looks in code:

```def is_leap(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

def next_leap(year):
next_possible = year - (year % 4) + 4
if is_leap(next_possible):
return next_possible
else:
while is_leap(next_possible) == False:
next_possible += 4
return next_possible```

Example run:

```year = int(input("Year: "))
print(f"The next leap year is {next_leap(year)}")```

Output:

```Year: 2021
The next leap year is 2024```

## Conclusion

A leap year is a year with 366 days. It occurs every four years on years divisible by 4 (but not on years divisible by 100 and not 400).

You can use the modulo to figure out leap years in Python.

```def is_leap(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)```

Thanks for reading. I hope you enjoyed it.

Happy coding!