Program to Check Leap Year in Python

Program to Check Leap Year in Python

04 Jun 2024
Beginner
1.66K Views
14 min read

Calculate the leap year in Python

The leap years area 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

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: 
else: 
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("{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 right 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 meansa 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.

Short Solution in Java: isLeap() method of Year class


import java.time.Year; 

public class LeapYear{ 
public static void main(String[] args) { 
int year = 2020 ; 

if (Year.of(year).isLeap()) 
System.out.println( year + " is a leap year . " ) ; 
} else { 
System.out.println( year + " is not a leap year . " ) ;
} 
}
}

Output


2024 is a leap year.

Explanation

Java's 'isLeap()' function makes leap year detection easier. It is part of the 'Year' class. You can quickly determine leap years in your Java programs by using this example. This shows your ability to use built-in Java libraries to do date-related operations with ease.
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

Q1. How to check leap Python?

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

Q2. Is 3000 a leap year?

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.

Q3. What is leap year logic?

 year % 4 == 0 && year % 100 != 0 || year % 400 == 0
Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
ASP.NET Core Certification Training Jul 17 MON, WED, FRI
Filling Fast
07:00AM to 08:30AM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training Jul 17 MON, WED, FRI
Filling Fast
07:00AM to 08:30AM (IST)
Get Details
Angular Certification Course Jul 20 SAT, SUN
Filling Fast
06:00PM to 08:00PM (IST)
Get Details
Generative AI For Software Developers Jul 20 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Azure Master Class Jul 20 SAT, SUN
Filling Fast
03:00PM to 05:00PM (IST)
Get Details
ASP.NET Core Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
Software Architecture and Design Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
.NET Solution Architect Certification Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
Azure Developer Certification Training Jul 28 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
Data Structures and Algorithms Training with C# Jul 28 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Angular Certification Course Aug 11 SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
ASP.NET Core Project Aug 24 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details

Can't find convenient schedule? Let us know

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