Program to Check Leap Year in Python

Program to Check Leap Year in Python

22 Aug 2024
Beginner
32.9K Views
14 min read

Calculate the leap year in Python

The leap years are a special year in our calendar. It just adds an extra day to February every four years. But, not every year gets this special extra day! Now I am sure you have got one question about how to recognize whether the year is a leap year or not. That's why I brought you a Python program that will determine the leap year.

So, In this Python Tutorial, We will calculate a leap year using Python which will contain rules for determining if a year is a leap year, The leap-year program in Python in detail with code examples, how to check Leap Year Using Macros, So Let's see one by one.

How to check a leap year?

The logic :

Now Look at the following program to understand its implementation in Python Compiler.

Example

def CheckLeap(Year):  
 # Checking if the given year is leap year  
  if((Year % 400 == 0) or (Year % 100 != 0) and (Year % 4 == 0)):   
    print("Yes! This Year is a leap Year");  
  # Else it is not a leap year  
  else:  
    print ("This Year is not a leap Year")  
# Taking an input year from user  
Year = int(input("Enter the number here: "))  
# Printing result  
CheckLeap(Year)    

Output

Enter the number here: 1700
Yes! This year is not a leap Year    

Explanation

See, We have executed all three conditions that we have above in the program using the 'and' and 'or' keywords inside the if else condition. After getting the input from the user, the program first, and then we will go to the input part and check if the given year is a leap year. If the condition is satisfied, the program will print 'Leap Year'; else the program will print 'Not a leap year.

The Leap Year Program in Python: For Loop

Let's calculate Leap year program using Python For loop.
def is_leap_year(year):
    if year % 4 == 0:
        if year % 100 == 0:
            if year % 400 == 0:
                return True
            else:
                return False
        else:
            return True
    else:
        return False

start_year = int(input("Enter the start year: "))
end_year = int(input("Enter the end year: "))

print("Leap years between", start_year, "and", end_year, "are:")
for year in range(start_year, end_year + 1):
    if is_leap_year(year):
        print(year)

Output

Enter the start year: 2020
Enter the end year: 2024
Leap years between 2020 and 2024 are:
2020
2024

Explanation

By applying the leap year logic and utilizing the Python code with the for loop provided, you'll be able to effortlessly determine whether a given year is a leap year or not.

Program to Check Leap Year: Macros

In Python, macros aren't a local concept like in languages such as C or C++. In any case, you'll achieve similar functionality utilizing functions or constants. Let's see how to create a Python program that checks for a leap year utilizing macros-like constants.

Creating Constants:

Now Let's define constants for the conditions of a leap year,
Leap_Condition1=4
Leap_Condition2=100
Leap_Condition3=400

Defining the Function of a Leap Year:

def is_leap year(year): 
if (year % Leap_Condition1 == 0 and year %
Leap_Condition2 ! = 0 ) or ( year % Leap_Condition3 == 0 ):
return True 
else : 
return False

Example

def is_leap_year(year):
    # Function to check if a year is a leap year
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
        return True
    else:
        return False

years_to_check = [2000, 2018, 2100, 2024]
for year in years_to_check:
    if is_leap_year(year):
        print(f"{year} is a leap year.")
    else:
        print(f"{year} is not a leap year.")

Output

2000 is a leap year 
2018 is not a leap year
2100 is not a leap year
2424 is a leap year
By executing a leap year checking function with constants that mimic macros, you'll be able to effectively decide leap years in your Python programs.

Python Short Solution of Leap Year Program

Determining leap years in Python can be achieved through various concise methods. Let's explore each of these in detail, complete with explanations and illustrative examples.

1. Leap Year Program in Python: Nested-if Condition

year = 2022
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.")

Output

2022 is not a leap year
The nested-if approach checks leap year conditions sequentially using nested if statements.

2. Leap Year Program in Python: if-else Condition

This method uses a single if-else statement to check leap year conditions directly.
year = 2020 
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.")

3. Leap Year Program in Python Using the Calendar Module

The calendar module's isleap() function simplifies leap year determination.
import calendar 
year = 2000 
if calendar.isleap(year): 
print(f"{year} is a leap year.") 
else : 
print(f" {year} is not a leap year.")

Leap Year Program in Python: While Loop

This determines if a particular year is a leap year or not. It continuously seeks the user for input using a Python while loop until they submit valid input. Here's a quick example:
while True:
    try:
        year = int(input("Enter a year: "))
        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.")
        break  # Exit the loop if valid input is provided
    except ValueError:
        print("Wrong Year. Please enter a valid year.")

Output

Enter a year: 2024
2024 is a leap year.

Explanation

Here, the while loop keeps running until the user enters a valid value meaning year, represented by an integer. Using the leap year logic, it determines whether the current year is a leap year and outputs the appropriate result.
Conclusion
So We have learned about both basic and sophisticated approaches through our exploration of several programs for calculating leap years in Python. However, to simplify development and up your game, learn the intricacies of Python with Scholarhat and level up as a Python developer.Also if you are preparing for interview don't forget to revise this top 50 Interview Questions and Answers in Python.

FAQs

We can check the leap year by checking the divisibility of the year with 4 and 400. 

3000 is a four-digit number that may also be divided by 100. However, because 3000 is divisible by 400, it will not be a leap year.

 year % 4 == 0 && year % 100 != 0 || year % 400 == 0
Share Article
About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.

Accept cookies & close this