How to Check if a Year is a Leap Year in Python 🐍


Check if a Year is a Leap Year in Python

A leap year is an extra-special year that comes every four years. It adds an additional day to the calendar—February 29th—to help synchronize the Earth’s orbit with the calendar year. In this blog, we will learn how to check if a given year is a leap year using Python.

By the end of this post, you will learn:

  • What a leap year is and why it happens.
  • The conditions for identifying a leap year.
  • How to write a Python program to check if a year is a leap year.

What is a Leap Year?

A leap year occurs every 4 years, except for years that are divisible by 100 but not divisible by 400. Here are the rules:

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

For example:

  • 2000 is a leap year (it is divisible by 400).
  • 1900 is not a leap year (it is divisible by 100 but not by 400).
  • 2024 is a leap year (it is divisible by 4 but not by 100).

How to Check if a Year is a Leap Year in Python

Let’s write a Python program that will take a year as input and check if it is a leap year based on the rules mentioned above.


Method 1: Using If-Else Statements

Code Example:

# Program to check if a year is a leap year

# Step 1: Take user input
year = int(input("Enter a year: "))

# Step 2: Apply the leap year conditions
if (year % 4 == 0):
    if (year % 100 == 0):
        if (year % 400 == 0):
            print(f"{year} is a leap year.")
        else:
            print(f"{year} is not a leap year.")
    else:
        print(f"{year} is a leap year.")
else:
    print(f"{year} is not a leap year.")

Explanation of the Code:

  1. User Input:
  • The user is prompted to enter a year, which is stored in the variable year.
  1. Leap Year Conditions:
  • We first check if the year is divisible by 4.
  • If it is, we then check if it is divisible by 100. If true, we also check if it is divisible by 400.
  • Based on these conditions, we print whether the year is a leap year or not.

Sample Output:

Enter a year: 2024
2024 is a leap year.
Enter a year: 1900
1900 is not a leap year.

Method 2: Using a Single If-Else Block

You can simplify the code using logical operators (and and or) to handle the conditions in a single if-else block.

Code Example:

# Program to check if a year is a leap year using a single if-else block

# Step 1: Take user input
year = int(input("Enter a year: "))

# Step 2: Apply the leap year conditions
if (year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
    print(f"{year} is a leap year.")
else:
    print(f"{year} is not a leap year.")

Explanation of the Code:

  1. User Input:
  • The user inputs a year, which is stored in the variable year.
  1. Leap Year Conditions:
  • We check if the year is divisible by 4 and, if it’s divisible by 100, we ensure that it is also divisible by 400. If both these conditions are true, the year is a leap year.
  1. Print the Result:
  • Based on the conditions, the program prints whether the year is a leap year or not.

Sample Output:

Enter a year: 2000
2000 is a leap year.
Enter a year: 2100
2100 is not a leap year.

Method 3: Using the calendar Module

Python’s calendar module has a built-in method called isleap() that checks whether a year is a leap year. This is a simple and efficient method if you prefer using built-in functions.

Code Example:

# Program to check if a year is a leap year using the calendar module

# Step 1: Import the calendar module
import calendar

# Step 2: Take user input
year = int(input("Enter a year: "))

# Step 3: Use the isleap() function to check if it's a leap year
if calendar.isleap(year):
    print(f"{year} is a leap year.")
else:
    print(f"{year} is not a leap year.")

Explanation of the Code:

  1. Import the calendar Module:
  • We import the calendar module, which provides the isleap() function.
  1. Check if the Year is a Leap Year:
  • The calendar.isleap(year) function returns True if the year is a leap year and False otherwise.
  1. Print the Result:
  • Based on the result, we print whether the year is a leap year or not.

Sample Output:

Enter a year: 2024
2024 is a leap year.
Enter a year: 2023
2023 is not a leap year.

Handling Special Cases

  1. Negative Years:
  • The program can also handle negative years, which can be useful when working with historical dates. For example, year -44 (44 BC) can be checked using the same logic.
  1. Year Zero:
  • Historically, there is no year zero in the Gregorian calendar (the one we use today). However, Python will treat year 0 as a valid input, so you may want to add an additional check if working with historical calendars.

Conclusion

In this blog, we explored three different ways to check if a year is a leap year in Python:

  1. Using nested if-else statements, which is the most basic approach.
  2. Using a single if-else block with logical operators for a more concise solution.
  3. Using Python’s calendar module, which provides the isleap() function for a quick and efficient check.

Understanding how leap years work is a good exercise in applying conditional logic, which is an essential skill in programming. Try out these methods and see which one you prefer!


Stay tuned for more Python programming tutorials! 😊

Share with our team

Leave a Comment